У меня есть 3 коллекции / подколлекции: questions
, questionsForUser
и questionsNotForUser
.
Каждый раз, когда в коллекцию questions
пишется, я хочу:
1)перебрать всех пользователей
2) установить подколлекцию пользователей questionsForUser
для всех документов в questions
, которые не отображаются в коллекции questionsNotForUser
.
Я пытаюсь сделать это вмашинописная облачная функция Google.
Как мне выполнить шаг 2?Вот что у меня есть:
export const updateUsersQuestions = functions.firestore.document('questions')
.onWrite((change, context) => {
try {
// get a reference to all question docs
const questionsCollectionRef = admin.firestore().collection('questions')
// create a batch for the transaction we will perform
const batch = admin.firestore().batch()
// loop through all users
admin.firestore().collection("users").get()
.then(collectionSnap => {
collectionSnap.forEach(userSnap => {
// get a reference to the users interaction_questions subcollection
var interactionQuestionsRef = userSnap.ref.collection("interaction_questions")
// get a reference to the question documents not for this user
var questionsNotForUserRef = admin.firestore().collection('questions_not_for_user')
// set the interactionQuestionsRef to all documents in questions collection that do not appear in questionsNotForUserCollection
// how do i do this?
//batch.set()
})
})
return batch.commit()
}
catch (error) {
console.log("Error updating user's quedstions", error)
}
})