我正在尝试从Firestore获取数据,当我控制台记录它时,我得到了我的集合内容,但是当我将代码移到一个函数中时,我无法将其返回。
这段代码可以工作:
const db = firebase.firestore();
db.settings({ timestampsInSnapshots: true});
db.collection('story').get().then((snapshot) => {
snapshot.docs.forEach(doc => {console.log(doc.data())
;})
})
这段代码不能正常工作(虽然它可以编译,但是没有返回任何值):
...
getMyStory = () => {
const db = firebase.firestore();
db.settings({ timestampsInSnapshots: true});
db.collection('story').get().then((snapshot) => {
snapshot.docs.forEach(doc => {
let items = doc.data();
})
});
return this.items;
}
render () {
return (
<p>{this.getMyStory}</p>
);
}
我做错了什么?
items.location
。这是不可能的,因为我们使用JSON.stringify()
函数将纯字符串存储在状态中。 - AnilJSON.parse()
来解决。如果我有错,请纠正我。 - Anil