Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

javascript - 检查Firestore文档中是否存在数据,否则将其推送到数组(check if data exists in firestore document else push to an array)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...