当我使用 where 时 Firestore 不会检索我的数据

玛妮·罗德里格斯

我正在尝试使用 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(any 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 语句将始终评估为真。

您可以将 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");
  }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当我使用 googleSignIn 时,用户在 Firestore 上重复

Flutter&Cloud_Firestore:使用2个“ where”条件时无法检索数据

当我们从 FireStore 检索数据时,tableview.reloaddata() 仍然在 Dispatchqueue.main.sync 之外工作

当我使用WHERE子句时,我的基本实体值为NULL

当我使用 Flutter Firestore 进行身份验证流程(登录、注销)时,如何为特定用户获取正确的数据?

当我订阅我的服务方法(执行查询)时,如何检索从 FireStore DB 中检索到的每个文档的 UID?

当我在asp.net中使用where条件时,SQL查询不起作用

当我在laravel的json列上使用where时返回空集合

当我使用带有 where 子句的 select 语句时,SqlDataReader 不返回任何行

为什么当我使用where /和筛选器强制转换getdate()时查询为空

为什么当我使用 whereRaw 和 mysql 中的 where 时得到不同的结果

从 sql 检索数据时,我想使用 where 条件作为变量

当我有总和时忽略WHERE子句的查询

当我尝试从Firestore更新数据“ FirebaseError:Function CollectionReference.doc()”时出现错误

当我使用变量时,抓取不会转移

当我使用 *ngFor (Angular 4) 时它不会呈现

当我使用.bashrc时颜色消失

当我使用setSupportActionBar()时,活动增加

当我更改创建的数据时,vuejs不会更新html

为什么我得到 Estimate Std。当我使用的数据永远不会是负数时为负数?

数据子句(当我使用 OpenACC 时输出为零)

当我使用onPostExecute推送mysql数据时出错

我如何在 .where() - Firestore React Native 中使用 veriable ?

当我使用 jQuery 代码时,我的导航栏消失了

为什么当我使用 <TouchableWithoutFeedback> 时我的设计消失了?

当我使用 vuetify 时,我无法显示图像

当我的 WHERE 子句过滤掉结果时,如何将结果包含在我的数据集中?

为什么我可以将枚举保存到 Firestore,但是当我尝试检索它时它会导致应用程序崩溃?

当我尝试在离子卡中显示Firestore中的特定文档时出错?