如何在Firestore上打破querySnapshot? [英] how to break querySnapshot on Firestore?

查看:30
本文介绍了如何在Firestore上打破querySnapshot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打破querysnapshot循环.有可能吗?

I need to break querysnapshot loop. Is it possible?

我尝试了for循环.但是出现以下错误.

I tried with for loop. but the below error is coming.

如何解决此错误,或者有什么方法可以打破快照循环?

How to fix this error or Is there any way to break snapshot loop?

代码

  return query.get()
    .then((snapshot) => {
      for(const doc of snapshot) {
        let data = doc.data()
        if (data.age == 16) {
            break;
        }
  }

错误

类型'QuerySnapshot'必须具有返回迭代器的'Symbol.iterator'方法.

Type 'QuerySnapshot' must have a 'Symbol.iterator' method that returns an iterator.

推荐答案

您可以使用

You can use the docs property of the QuerySnapshot, which returns an array of all the documents in the QuerySnapshot.

例如,带有for循环:

For example, with a for loop:

  return query.get()
    .then((snapshot) => {
      const snapshotsArray = snapshot.docs;
      for (var i = 0; i < snapshotsArray.length; i++) {
        const data = snapshotsArray[i].data()
        if (data.age == 16) {
            break;
        }
      }
  }

或带有 for :

  return query.get()
    .then((snapshot) => {
      const snapshotsArray = snapshot.docs;
      for (const snap of snapshotsArray) {
        const data = snap.data()
        if (data.age == 16) {
            break;
        }
      }
  }

这篇关于如何在Firestore上打破querySnapshot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆