【发布时间】: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