如何突破querySnapshot forEach循环方法?

时间:2018-09-10 02:08:07

标签: javascript node.js firebase google-cloud-firestore

我想打破forEach循环。考虑一下:

db.collection('users').get().then(querySnapshot => {
        if (!querySnapshot.empty) {
            querySnapshot.forEach(doc => {
                let data = doc.data()
                if (data.age == 16) {
                    break //this is not working
                }
            })
        }
    }
)

documentation并没有提及打破forEach循环

3 个答案:

答案 0 :(得分:7)

您可以直接在QuerySnapshot上使用forEach属性,而不是直接在QuerySnapshot上使用docs,这只是一个普通的旧javascript数组。编写一个for循环,您可以根据需要break

for (var i in querySnapshot.docs) {
    const doc = querySnapshot.docs[i]
    if (make_some_decision_here) {
        break
    }
}

答案 1 :(得分:0)

根据此处https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

  

除了引发异常外,没有其他方法可以停止或中断forEach()循环。如果您需要这种行为,则forEach()方法是错误的工具。

停止或中断forEach()循环的唯一方法是引发异常
尝试这些替代方法
一个简单的循环
for ... of循环
Array.prototype.every()
Array.prototype.some()
Array.prototype.find()
Array.prototype.findIndex()

答案 2 :(得分:0)

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Track01Track : MonoBehaviour { public GameObject TheMarker; public GameObject Mark01; public GameObject Mark02; public GameObject Mark03; public GameObject Mark04; public GameObject Mark05; public GameObject Mark06; public GameObject Mark07; public GameObject Mark08; public GameObject Mark09; public GameObject Mark10; public GameObject Mark11; public GameObject Mark12; public GameObject Mark13; public GameObject Mark14; public GameObject Mark15; public GameObject Mark16; public GameObject Mark17; public GameObject Mark18; public GameObject Mark19; public GameObject Mark20; public GameObject Mark21; public GameObject Mark22; public GameObject MarkTracker; void Start () { } void Update () { if (MarkTracker == 0) { TheMarker.transform.position = Mark01.transform.position; } if (MarkTracker == 1) { TheMarker.transform.position = Mark02.transform.position; } if (MarkTracker == 2) { TheMarker.transform.position = Mark03.transform.position; } if (MarkTracker == 3) { TheMarker.transform.position = Mark04.transform.position; } if (MarkTracker == 4) { TheMarker.transform.position = Mark05.transform.position; } if (MarkTracker == 5) { TheMarker.transform.position = Mark06.transform.position; } if (MarkTracker == 6) { TheMarker.transform.position = Mark07.transform.position; } if (MarkTracker == 7) { TheMarker.transform.position = Mark08.transform.position; } if (MarkTracker == 8) { TheMarker.transform.position = Mark09.transform.position; } if (MarkTracker == 9) { TheMarker.transform.position = Mark10.transform.position; } if (MarkTracker == 10) { TheMarker.transform.position = Mark11.transform.position; } if (MarkTracker == 11) { TheMarker.transform.position = Mark12.transform.position; } if (MarkTracker == 12) { TheMarker.transform.position = Mark13.transform.position; } if (MarkTracker == 13) { TheMarker.transform.position = Mark14.transform.position; } if (MarkTracker == 14) { TheMarker.transform.position = Mark15.transform.position; } if (MarkTracker == 15) { TheMarker.transform.position = Mark16.transform.position; } if (MarkTracker == 16) { TheMarker.transform.position = Mark17.transform.position; } if (MarkTracker == 17) { TheMarker.transform.position = Mark18.transform.position; } if (MarkTracker == 18) { TheMarker.transform.position = Mark19.transform.position; } if (MarkTracker == 19) { TheMarker.transform.position = Mark20.transform.position; } if (MarkTracker == 20) { TheMarker.transform.position = Mark21.transform.position; } if (MarkTracker == 21) { TheMarker.transform.position = Mark22.transform.position; } } IEnumerator OnTriggerEnter(Collider collision) { if (collision.gameObject.tag == "Track01") { this.GetComponent<BoxCollider>().enabled = false; MarkTracker += 1; if (MarkTracker == 22) { MarkTracker = 0; } yield return new WaitForSeconds(1); this.GetComponent<BoxCollider>().enabled = true; } } } 不提供提早退出的直接机制。最好的选择是抛出一个异常:

.forEach

或者,您可以使用常规的for循环方法来简化外观:

querySnapshot => {
  if (!querySnapshot.empty) {

    try {

      querySnapshot.forEach(doc => {
          let data = doc.data()
          if (data.age == 16) {
              // Throw an exception to break. Use a custom exception type to
              // distinguish it from other possible exceptions that could be
              // thrown
              throw EarlyExit; 

          }
      })

    } catch(exception) {

      // If the exception thrown is not our EarlyExit type, then re throw,
      // otherwise resume as if look with terminated
      if(exception !== EarlyExit) {
        throw exception
      }
    }
  }
}