如何在firebase中访问以随机ID命名的子项?

时间:2021-12-09 02:46:46

I want to be able to automatically access every child named with random ID keys nested inside "posts", is there a way to get the children of every child inside them?

我希望能够自动访问嵌套在“帖子”中的随机ID键命名的每个孩子,有没有办法让每个孩子的孩子都在里面?

如何在firebase中访问以随机ID命名的子项?

I am completely new to firebase and managing database in general (started learning code 3 months ago), so I might have missed it in the documentation out of confusion rather than just dismissing it

我是firebase和管理数据库的新手(3个月前开始学习代码),所以我可能在文档中错过了它而不仅仅是解雇它

2 个解决方案

#1


0  

Do you need the actual random object key ID? Or just the properties for each object (typically, the properties are what you need, not the object ID)!

您需要实际的随机对象密钥ID吗?或者只是每个对象的属性(通常,属性是您需要的,而不是对象ID)!

const arrayOfObjects = test2-2271b.posts[0];

arrayOfObjects.map(function(elem){
  console.log(elem.caption) // returns the caption value for each object
  console.log(elem.profilePic)
  console.log(elem.username)
})

#2


0  

When you use push() method in firebase it will automatic generate unique key for your record.

在firebase中使用push()方法时,它会自动为您的记录生成唯一键。

The unique key is based on a timestamp, so list items will automatically be ordered chronologically. Because Firebase generates a unique key for each blog post, no write conflicts will occur if multiple users add a post at the same time.

唯一键基于时间戳,因此列表项将按时间顺序自动排序。由于Firebase会为每篇博客帖子生成唯一键,因此如果多个用户同时添加帖子,则不会发生写入冲突。

use set() or update() method to create your own custom keys.

使用set()或update()方法创建自己的自定义键。

below is sample code to get key of your 1st record

下面是获取第一条记录密钥的示例代码

var ref = firebase.database().ref("posts/j5fp0kl*********");
ref.on('value', function(snapshot){
    var dataList = snapshot.val();
    var key = Object.keys(dataList)[0]; //this will return 1st key.         
    var keys = Object.keys(dataList);   //this will return all keys.
});

#1


0  

Do you need the actual random object key ID? Or just the properties for each object (typically, the properties are what you need, not the object ID)!

您需要实际的随机对象密钥ID吗?或者只是每个对象的属性(通常,属性是您需要的,而不是对象ID)!

const arrayOfObjects = test2-2271b.posts[0];

arrayOfObjects.map(function(elem){
  console.log(elem.caption) // returns the caption value for each object
  console.log(elem.profilePic)
  console.log(elem.username)
})

#2


0  

When you use push() method in firebase it will automatic generate unique key for your record.

在firebase中使用push()方法时,它会自动为您的记录生成唯一键。

The unique key is based on a timestamp, so list items will automatically be ordered chronologically. Because Firebase generates a unique key for each blog post, no write conflicts will occur if multiple users add a post at the same time.

唯一键基于时间戳,因此列表项将按时间顺序自动排序。由于Firebase会为每篇博客帖子生成唯一键,因此如果多个用户同时添加帖子,则不会发生写入冲突。

use set() or update() method to create your own custom keys.

使用set()或update()方法创建自己的自定义键。

below is sample code to get key of your 1st record

下面是获取第一条记录密钥的示例代码

var ref = firebase.database().ref("posts/j5fp0kl*********");
ref.on('value', function(snapshot){
    var dataList = snapshot.val();
    var key = Object.keys(dataList)[0]; //this will return 1st key.         
    var keys = Object.keys(dataList);   //this will return all keys.
});