Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break out of the querySnapshot forEach loop method?

I want to break out of a forEach loop. Consider this:

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
                }
            })
        }
    }
)

The documentation mentions nothing about breaking out the forEach loop

like image 929
TSR Avatar asked Sep 10 '18 02:09

TSR


1 Answers

Instead of using forEach on QuerySnapshot directly, you could iterate its docs property instead, which is just a plain old javascript array. Write a for loop, which you can break as needed:

for (var i in querySnapshot.docs) {
    const doc = querySnapshot.docs[i]
    if (make_some_decision_here) {
        break
    }
}
like image 178
Doug Stevenson Avatar answered Sep 30 '22 21:09

Doug Stevenson