Reactjs 如何调试此Firestore/React表达式?

Reactjs 如何调试此Firestore/React表达式?,reactjs,firebase,google-cloud-firestore,Reactjs,Firebase,Google Cloud Firestore,我不理解Firestore中“.doc”和/或“doc”的含义,因此无法获取我的文档参考 const getIt = () => { setLoading(true); const item = []; const docRef = firebase .firestore() .collection("polja") .doc("id", "==", match.para

我不理解Firestore中“.doc”和/或“doc”的含义,因此无法获取我的文档参考

const getIt = () => {
    setLoading(true);
    const item = [];
    const docRef = firebase
      .firestore()
      .collection("polja")
      .doc("id", "==", match.params.id);
    //
    console.log(docRef);
    //
    docRef.onSnapshot((doc) => {
      setItem(doc.data());
      // //
      setLoading(false);
    });
  };
当您知道文档ID时,必须使用
.doc()
,然后查询如下:

const docRef = firebase.firestore().collection("polja").doc("documentId");
但是,如果要查找具有特定字段值的文档,则需要查找
。where()

这可以匹配多个文档,其中该字段的值与提供的值匹配。因此,要控制返回的数字或结果,请使用或

如果使用
.doc()
函数,则响应中的变量
doc
是DocumentSnapshot。然后可以使用
doc.data()
获取其数据

另一方面,如果使用
.where()
,它将返回
FirebaseFirestore.QuerySnapshot
,其中包含与在.where()中指定的条件匹配的所有文档。要访问该QuerySnapshot中的所有文档,必须访问docs属性,如下所示:
snapshot.docs
。 现在,这将是一个文档数组。您可以使用
snapshot.size
查看有多少文档与您的查询相匹配

需要澄清的一些实例:

//Collection in Firestore:
users -> {userID as documentId} -> user data in the document

等一下-是你告诉我在查找单个文档时“.where”没有任何用处的@ydrea但您能否确认
匹配.params.id
与文档id相同,并且只有一个文档具有该id?如果是,请使用
.doc(match.params.id)
.doc()
只接受1个参数,即文档ID。如果您有疑问,您将传递3个参数。@ydrea我添加了更多解释,如果您知道该文档的ID,请使用
.doc()
。完成,谢谢@太好了!如果您的问题已经解决,您可以接受答案,也可以提出进一步的问题。
//Collection in Firestore:
users -> {userID as documentId} -> user data in the document
//In this case you know the userID then you can should use the .doc()
const userDocRef = firebase.collection("users").doc("userId");
userDocRef.get().then((userData) => {
  console.log(userData.data())
  //^ this will contain data in side that document
})

//In this case, you want to find Firestore documents based on value of any field present in the document, lets say in this example if user's age is more than 15
const userDocsRef = firebase.collection("users").where("age", ">=", 15);
userDocsRef.get().then((usersData) => {
  console.log(usersData.docs)
  //^ this will log an array of documents which matched the condition of having the value of field age greater than or equal to 15
})