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
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With