Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.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/2/joomla/2.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
Javascript 如何从react中的firestore获取特定id的数据?_Javascript_Reactjs_Google Cloud Firestore_React Hooks - Fatal编程技术网

Javascript 如何从react中的firestore获取特定id的数据?

Javascript 如何从react中的firestore获取特定id的数据?,javascript,reactjs,google-cloud-firestore,react-hooks,Javascript,Reactjs,Google Cloud Firestore,React Hooks,我试图弄清楚如何从firestore中仅从一个id获取数据。当单击我的编辑按钮时,我希望我的react网站转到另一个页面,在该页面中,我的输入字段中填写了特定id中的值。单击编辑按钮后,我解决了如何从url中的firestore中的特定id获取我的id 如何仅从我单击的特定id/url中弹出的相同id获取数据 可以在url中获取规范id 但当我试图从firestore获取特定id的所有数据时,我失败了。这是我失败的代码 const _id = props.match.params.id; con

我试图弄清楚如何从firestore中仅从一个id获取数据。当单击我的编辑按钮时,我希望我的react网站转到另一个页面,在该页面中,我的输入字段中填写了特定id中的值。单击编辑按钮后,我解决了如何从url中的firestore中的特定id获取我的id

如何仅从我单击的特定id/url中弹出的相同id获取数据

可以在url中获取规范id

但当我试图从firestore获取特定id的所有数据时,我失败了。这是我失败的代码

const _id = props.match.params.id;
console.log("Logged data in edit:", _id);

const ref = firebase.firestore().collection("suggestions").doc(_id); 

function getRestaurants() {
    ref.get().then(querySnapshot => setData((querySnapshot).data()))
   console.log(ref.data)
}

我正在获取数据,但无法写入/编辑输入字段

.doc().get()不会返回querySnapshot,它会返回DocumentSnapshot,我认为您的“``(querySnapshot).data()``并不能完全满足您的需要。如果没有关于该片段范围/上下文的更多信息,很难说什么是有效的。我假设setData已定义。。。在什么地方

此外,ref.data()不是一件事
-ref不会被.get()方法更改或更新。同样地,.get()的结果(它不是查询)在.then()之外不可用

我希望您至少需要:


我在你的代码中得到了完全相同的结果。编辑/编写HTML字段(我相信你是在问这个问题)与Firestore完全没有任何关系,而你在这里没有显示任何内容。
const _id = props.match.params.id;
console.log("Logged data in edit:", _id);

const ref = firebase.firestore().collection("suggestions").doc(_id); 

function getRestaurants() {
    ref.get().then(DocumentSnapshot => {
       const docData = querySnapshot.data();
       setData(docData);
      console.log(docData);
   })
}