Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在react native中使用firebase显示来自留言板的消息_Node.js_Reactjs_Firebase_React Native_Google Cloud Firestore - Fatal编程技术网

Node.js 在react native中使用firebase显示来自留言板的消息

Node.js 在react native中使用firebase显示来自留言板的消息,node.js,reactjs,firebase,react-native,google-cloud-firestore,Node.js,Reactjs,Firebase,React Native,Google Cloud Firestore,我是firebase的新手,我正在关注youtube上关于如何使用firebase实时显示消息的教程。我没有收到任何错误,我无法从云firestore检索消息。有没有人能帮我指出正确的方向,因为我已经尽了全力,但似乎没有任何效果。下面是我的firestore数据库的屏幕截图,以及为避免任何复杂性而精简的代码 构造函数(道具){ 超级(道具); 此.state={ 消息:“”, 信息:[], }; } componentDidMount(){ 火基 .数据库() .ref() .child('/

我是firebase的新手,我正在关注youtube上关于如何使用firebase实时显示消息的教程。我没有收到任何错误,我无法从云firestore检索消息。有没有人能帮我指出正确的方向,因为我已经尽了全力,但似乎没有任何效果。下面是我的firestore数据库的屏幕截图,以及为避免任何复杂性而精简的代码

构造函数(道具){
超级(道具);
此.state={
消息:“”,
信息:[],
};
}
componentDidMount(){
火基
.数据库()
.ref()
.child('/messageboard/gFmLa20cKCzXilXSDGGq')
.once('value',快照=>{
const data=snapshot.val();
如果(数据){
const initMessages=[];
对象
.钥匙(数据)
.forEach(message=>initMessages.push(data[message]);
这是我的国家({
消息:initMessages
});
}
});
火基
.数据库()
.ref()
.child('/messageboard/gFmLa20cKCzXilXSDGGq')
.on('child_added',snapshot=>{
const data=snapshot.val();
如果(数据){
this.setState(prevState=>({
消息:[数据,…prevState.messages]
}));
}
});
}
renderItem({item}){
返回(
{item}
);
}
render(){
const{message,messages}=this.state;
让emptyComponent=null;
emptyComponent=(
没有找到任何消息
);
返回(
);
}
}

您似乎正在使用firebase代码检索使用Firestore存储的数据。虽然它们都是firebase提供的数据库,但它们的结构和访问信息的方式不同

要一次性获取消息,请执行以下操作:

db.collection("message-board").doc("gFmLa20cKCzXilXSDGGq")
    .get()
    .then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            // doc.data() is never undefined for query doc snapshots
            console.log(doc.id, " => ", doc.data());
        });
    })
    .catch(function(error) {
        console.log("Error getting documents: ", error);
    });
摘自以下文件:

由于您正在查找实时更新,因此可能会出现以下情况:

db.collection("message-board").doc("gFmLa20cKCzXilXSDGGq")
    .onSnapshot(function(doc) {
        console.log("Current data: ", doc.data());
    });
摘自:

db.collection("message-board").doc("gFmLa20cKCzXilXSDGGq")
    .onSnapshot(function(doc) {
        console.log("Current data: ", doc.data());
    });