【问题标题】:Firebase Firestore Query get One resultFirebase Firestore 查询得到一个结果
【发布时间】:2020-02-12 22:16:14
【问题描述】:

我正在寻找最好的方法:
1. 查询一个结果,或者
2.从查询中提取第一个结果

试过了:collection[0]collection.pop()collection.shift() 没有任何效果

我真的不喜欢我正在使用的代码,但它可以工作......

export const findUserByEmail = email => {
  return firestore()
    .collection('users')
    .where('email', '==', email.toLowerCase())
    .get()
    .then(collection => {
      console.log(collection)
      let user
      collection.forEach(doc => user = doc.data())
      return user
    })
}

【问题讨论】:

  • 您的文字显示为result,但您的代码显示为collection。这只是一个错字吗?
  • 谢谢,是的,这是一个错字。立即编辑

标签: javascript firebase google-cloud-firestore


【解决方案1】:

您的查询返回QuerySnapshot (see docs)。您可以通过 docs 属性以数组形式访问文档;每个文档都有一个data() 方法:

export const findUserByEmail = email => {
  return firestore()
    .collection('users')
    .where('email', '==', email.toLowerCase())
    .get()
    .then(querySnapshot => {
      if(!querySnapshot.empty) {
        const user = querySnapshot.docs[0].data()
        // rest of your code 
      }
    })
}

【讨论】:

  • 解决方案是 .then(snap => snap.docs[0].data()) 谢谢
  • 是谷歌获取第一个文档的“querySnapshot.docs[0]”标准吗?如果是,那么这真的是非常糟糕的编程。我不是在责怪你“查尔斯-艾伦”。应该有内置函数,比如使用“.get()”获取文档列表,只获取第一个文档使用“.first()”。我不知道谷歌有“.first()”或任何类似的功能。它只是基本的东西,我认为每个框架/ CMS 都有这样的功能。例如。 Laravel / Lumen 具有相同的“get()”和“first()”函数。我们正在破解代码以通过“querySnapshot.docs[0].data()”获取第一个文档。
  • "if(!querySnapshot.empty) {" 对我不起作用,因为没有“空”功能,所以我检查了文档长度。请看代码: if(querySnapshot.docs.length > 0) { _mappedUserInfo = querySnapshot.docs[0].data(); }
  • @kamlesh - 您的错误消息表明您将其称为querySnapshot.empty()(不正确)。它是一种属性而不是一种方法。像这样使用它:querySnapshot.empty。见the docs here。如果你更喜欢docs.length,那很好;只是更长。
【解决方案2】:

Firestore 与 Flutter 解决方案:

await FirebaseFirestore.instance
        .collection('users')
        .where('userId', isEqualTo: loggedInUserId)
        .get()
        .then((querySnapshot) {
      List listData = querySnapshot.docs.toList().map((item) {
        // to fetch list of all documents information
        return item.data();
      }).toList();

      if (querySnapshot.docs.length > 0) {
        // to fetch first document information
        _mappedUserInfo = querySnapshot.docs[0].data();
      }

      print("listData: ${listData.toString()}");
      print("_mappedUserInfo: ${_mappedUserInfo.toString()}");
    }).catchError((e) {
      print('data error: $e');
    });

结果:

获取的文档列表:

listData: [{name: null, addedOn: Timestamp(seconds=1623663004, nanoseconds=581000000), email: john.doe2021@mailinator.com, phoneNumber: null, userId: yRARGJOaAbM78MW5OBlVEB3VyKkr, lastSignInOn: Timestamp(seconds=1623663004, nanoseconds=581000000), photoURL: null}]

获取的单个文档:

_mappedUserInfo: {name: null, addedOn: Timestamp(seconds=1623663004, nanoseconds=581000000), email: john.doe2021@mailinator.com, phoneNumber: null, userId: yRARGJOaAbM78MW5OBlVEB3VyKkr, lastSignInOn: Timestamp(seconds=1623663004, nanoseconds=581000000), photoURL: null}

它对我有用。希望对您有所帮助。

【讨论】:

    【解决方案3】:

    试过了:collection[0]collection.pop() 没有任何效果

    这2个按顺序表示:

    1. collection[0] - 访问数组的第一个元素
    2. collection.pop() - pop 摆脱最后一个 数组元素

    也许根据你的代码逻辑,你应该试试:collection[0].data();通过使用collection[0],您可以访问第一个元素,但您需要从现有代码中调用.data() 来提取它。

    forEach 只是访问collection 的全部内容。 虽然 collection.pop() 在这种情况下不是适合这项工作的工具。

    【讨论】:

    • collection 不是一个数组,它是一个 firebase 对象,有它自己的属性。它有一个 forEach 的事实是一个巧合......所以 collection[0] 并没有让我得到任何东西。 collection.pop 不存在,与 .shift 相同。我知道pop是什么意思,只是我确信如果有结果,它总是一个,所以没关系
    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 2021-10-28
    • 1970-01-01
    • 2021-11-13
    • 2021-09-25
    • 2018-05-18
    • 2014-08-18
    • 1970-01-01
    相关资源
    最近更新 更多