Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.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 当我使用where时,Firestore不会检索我的数据_Node.js_Reactjs_Firebase_Google Cloud Firestore - Fatal编程技术网

Node.js 当我使用where时,Firestore不会检索我的数据

Node.js 当我使用where时,Firestore不会检索我的数据,node.js,reactjs,firebase,google-cloud-firestore,Node.js,Reactjs,Firebase,Google Cloud Firestore,我正在尝试使用where从数据库中检索数据。用户输入他们想要邀请的电子邮件,然后发送邀请。但是,没有返回任何文档。这是我用来检索的代码: await props.firestore .collection("users") .where("email", "==", state.inviteEmail) .get() .then((doc) => { if (!doc.exists) { props.showMsg(

我正在尝试使用where从数据库中检索数据。用户输入他们想要邀请的电子邮件,然后发送邀请。但是,没有返回任何文档。这是我用来检索的代码:

await props.firestore
    .collection("users")
    .where("email", "==", state.inviteEmail)
    .get()
    .then((doc) => {
      if (!doc.exists) {
        props.showMsg(
          "No user with that email exists. Make sure you've entered it correctly"
        );
      } else {
        // This is where it goes if it works
        alert("It works");
      }
    });
我尝试用.doc(任何id)替换where,它可以正常工作,因此我知道数据库连接正确。这是数据库的视图:

我使用的是Reactjs,数据库使用的是Firebase

编辑:对于其他有相同问题的人,这是我所做的更改:

await props.firestore
    .collection("users")
    .where("email", "==", state.inviteEmail)
    .get()
    .then((doc) => {
      if (doc.empty) {
        props.showMsg(
          "No user with that email exists. Make sure you've entered it correctly"
        );
      } else {
        alert("This emails username: "+doc.docs[0].data().username);
      }
    });

数据将作为QuerySnapshot返回,它类似于一个数组,因此它需要是doc.docs[0].data,而不是doc.data()。因为我只有一个保证返回的文档,所以我只抓取了第一个元素,但是如果我有多个元素,我就必须使用foreach。

当您添加
where
子句时,您实际上得到的是
QuerySnapshot
对象,而不是
DocumentSnapshot
。因此,代码中的问题是
QuerySnapshot
对象没有
exists
字段作为
DocumentSnapshot
对象。因此,在这种情况下,
doc.exists
始终等于
undefined
,if语句的计算结果始终为true

您可以将if条件转换为查找
empty
属性,因此代码如下所示:

firestore
.collection("users")
.where("email", "==", state.inviteEmail)
.get()
.then((snap) => {
  if (snap.empty) {
    props.showMsg(
      "No user with that email exists. Make sure you've entered it correctly"
    );
  } else {
    // This is where it goes if it works
    alert("It works");
  }
});

我之前试过,但我一直得到“doc.data不是函数”,这意味着它无法获取任何东西。我已经替换了通知,说它与props.showMsg(“电子邮件的用户名:“+doc.data().username”)一起工作@Lonstu这是因为
QuerySnapshot
对象没有
data
函数。您将
QuerySnapshot
与前面提到的
DocumentSnapshot
对象混淆。试着把
QuerySnapshot
看作是
DocumentSnapshot
的集合,还有一些其他属性和方法,例如用于迭代的
forEach
和保存返回文档数量的
size
。为了更好地理解,请浏览firestore上的
QuerySnapshot
谢谢。我不知道返回的是QuerySnapshot,它被当作数组处理。我使用了doc.docs[0].data().username,它运行正常。再次感谢你!我只是在标题上加了“已解决”一词,就像我在其他问题上看到的那样。我还包括了我的问题的解决方案,因为我觉得这有助于解释为什么我首先会遇到这个问题。每当我在这个网站上遇到问题时,必须通过多个链接来找出他们是如何解决问题的,这可能会让我感到沮丧,因此我觉得如果我将解决方案放在我的网站上,它可能会在将来更有效地帮助其他人。我不知道堆栈溢出有这样的规则,没关系。这就是为什么我解释了什么和为什么。关于这个话题的一些讨论,你可能会觉得更可信:哦,我明白了,我明白你的意思。Tbh我不知道我可以发布一个关于我自己问题的答案