使用与云功能中两个其他集合相交的所有文档更新Firestore集合

时间:2019-03-06 04:18:37

标签: typescript google-cloud-firestore google-cloud-functions

我有3个集合/子集合:questionsquestionsForUserquestionsNotForUser

每次将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)
        }
    })

0 个答案:

没有答案