Если я правильно понимаю, вы хотите получить массив userSnaphot
документов, отфильтрованных по следующим критериям:
learning_language == doc.data().spoken_language
Одним из подходов здесь может быть использование метода Array # filter () для достижения этой цели, при котором вы будете использовать критерии соответствия, указанные выше, для фильтрации пользовательских документов, которые соответствуют требуемому learning_language
.
Чтобы получить отфильтрованный массив пользовательских документов в качестве результирующей переменной ns_match
, вам необходимо разрешить массив обещаний, поскольку вы выполняете обещание db.collection(ns_profile).doc(userSnapshot.id).get()
для каждого элемента в массиве snapshot.docs
.
Для этого можно использовать функцию Promise.all()
, как подробно описано в комментариях ниже:
db.collection(ns_status)
.where("state", "==", "online")
.onSnapshot(function(snapshot) {
/*
Return the promise so that resolved ns_match array can be used externally
*/
return Promise.all(snapshot.docs.map((userSnapshot) => {
/*
For each document in docs, map each to a promise. Once all of
these promises are resolved, Promise.all() will pass the array
of documents to the next then() handler
*/
return db.collection(ns_profile).doc(userSnapshot.id).get();
}))
.then(users => {
/*
We now have a list of user documents in users. It seems like
the filter method is better suited to what you're wanting, so
filter user documents based on the condition that
spoken_language === learning_language
*/
return users.filter(doc => {
const spoken_language = doc.data().spoken_language;
return learning_language === spoken_language;
});
})
.then(ns_match => {
/*
We now recieve the list of filtered user documents where the
language ns_match applies for all items in ns_match
*/
console.log(ns_match);
/* Return this filtered array for use outside of this call */
return ns_match;
})
})
Надеюсь, это поможет!