【问题标题】:Can't get object key in firebase无法在firebase中获取对象键
【发布时间】:2018-04-30 16:45:41
【问题描述】:

问题:我正在使用 Firebase 和 React Native 制作一个应用程序,用户可以在其中发帖,然后其他用户可以对帖子发表评论。要进行 cmets,我需要获取帖子内容的关键。我尝试使用 child.getKey() 函数,但这给了我一个错误。

child.getKey is not a function. (In 'child.getKey()', 'child.getKey' is undefined)

我真的很想得到钥匙的帮助。谢谢!

代码

  getItems(){
    var items = [];
    var query = ref.orderByKey();
    query.once ('value', (snap) => {
      snap.forEach ( (child) => {       
       items.push({
         content: child.val().content,
         key: child.getKey()
     });
     });
    items.reverse();
    }).then(() => {
        this.setState({firebaseItems: items});
    });
 }

Firebase 布局

Posts:
    -Kier498dma39md:
        content: 'This is an example post. The numbers above me are an example of the random key I am trying to get.'

【问题讨论】:

  • 如果您记录 snap 和 child 会发生什么?那些是什么?对象?函数?
  • 我使用了 FirebaseListAdapter。在我的列表视图的 setOnItemClickListener 中,我使用了 firebaseListAdapter.getRef(i);获取对对象的引用(其中 i 是项目在列表中的位置)。

标签: javascript firebase react-native firebase-realtime-database


【解决方案1】:

所以,我做了一些研究,最终发现我需要做的就是使用key: child.key

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-17
    • 2018-05-11
    • 1970-01-01
    • 2015-03-24
    • 2017-03-13
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多