I want to check if the data exists in the firestore document, if it does not, then I want to push it to an array.
(我想检查数据在Firestore文档中是否存在,如果不存在,那么我想将其推送到数组中。)
In the code below, I want to check if q1,q2,q3,q4,q5 exists in the database.
(在下面的代码中,我要检查数据库中是否存在q1,q2,q3,q4,q5。)
The database is structured like this:(数据库的结构如下:)
firestore /users/userID/post/PlMGiyQRDZuKGipTu5sl/
{ question:'q2'
answer: 'answer to q2'
}
let questions = [q1,q2,q3,q4,q5];
let questions_subset = [];
questions.forEach((item, index) => {
let query = db.collection('users').doc('userID').collection('post').where('question', '==', item).get();
query.then(snapshot => {
if (snapshot.empty) {
console.log('No matching documents.');
questions_subset.push(item);
return;
}
snapshot.forEach(doc => {
console.log(doc.id, '=>', doc.data());
});
return
}).catch(err => {
console.log('Error getting documents', err);
});
console.log(questions_subset);
})
Right now, console.log(questions_subset) gives me an empty array.
(现在,console.log(questions_subset)给我一个空数组。)
What am I doing wrong?(我究竟做错了什么?)
ask by maharrx translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…