我有3个集合/子集合:questions
,questionsForUser
和questionsNotForUser
。
每次将questions
集合写给我想
1)遍历所有用户
2)将用户questionsForUser
子集合设置为questions
中所有未出现在questionsNotForUser
集合中的文档。
我正在尝试使用打字稿Google Cloud函数执行此操作。
如何实现第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)
}
})