【问题标题】:Firestore Cloud Function Query returning value after database query?Firestore Cloud Function Query在数据库查询后返回值?
【发布时间】:2019-01-05 03:41:38
【问题描述】:

目前,我正在查询数据库中要更新的对象。这样做的函数的 sn-p 是

 return getPostsForDate.get().then(snapshot => {
        const updates = {}
        var counter = 0
        const batch = admin.firestore().batch()
        snapshot.forEach((doc) => {

            var key = doc.id
            return admin.database().ref('/convoID/' + key).once('value', (snapshot) => {
                if (snapshot.exists()) {
                    const convoIDCollection = snapshot.val()
                    for (var child in convoIDCollection) {

                        console.log(child)
                        updates["conversations/" + child] = null
                        updates["messages/"+ child] = null
                        updates["convoID/"+ child] = null
                    }
                }
                updates["/convoID/" + key] = null
                updates["/reveals/" + key] = null
                updates["/postDetails/" + key] = null
                const postFireStoreRef = admin.firestore().collection('posts').doc(key)
                const posterRef = admin.firestore().collection('posters').doc(key)
                batch.delete(postFireStoreRef)
                batch.delete(posterRef)
                counter++
                console.log(counter)
             })

        })
        if (counter > 0) {
            console.log("at the deletion point")
              return Promise.all[admin.database().ref().update(updates), batch.commit()] 
        }
        else {
            console.log("null")
            return null
        }
})

本质上,在 firestore 查询帖子后,会从实时数据库接收更多详细信息并将其添加到数组中。最后,我通过一个承诺提交所有这些更新。但是,永远不会到达返回更新的函数——为了确保数据库需要更新,我有一个计数器来计算更新的数量。如果大于0,我返回

 return Promise.all[admin.database().ref().update(updates), batch.commit()]

但是,返回函数似乎是在收到其他详细信息之前执行的,因为它不断将“null”返回到控制台日志,只有当计数器小于 0 时才会发生这种情况。

本质上,如何在执行更新之前等待查询数据?

更新

    return getPostsForDate.get().then(snapshot => {
    const updates = {} 
    const promises = []
    var counter = 0
    const batch = admin.firestore().batch()
    snapshot.forEach((doc) => {
 promises.push (
        admin.database().ref('/convoID/' + key).once('value', (snapshot) => {
            if (snapshot.exists()) {
                const convoIDCollection = snapshot.val()
                for (var child in convoIDCollection) {
                    updates["conversations/" + child] = null
                    updates["messages/"+ child] = null
                    updates["convoID/"+ child] = null
                }
            }
            updates["/convoID/" + key] = null
            updates["/reveals/" + key] = null
            updates["/postDetails/" + key] = null
            const postFireStoreRef = admin.firestore().collection('posts').doc(key)
            const posterRef = admin.firestore().collection('posters').doc(key)
            batch.delete(postFireStoreRef)
            batch.delete(posterRef)
            counter++
         })
        )
    })
    Promise.all(promises).then(() => {
    if (counter > 0) {
        console.log("at the deletion")
          return Promise.all[admin.database().ref().update(updates), batch.commit()] 
    }
    else {
        console.log("null")
        return null
    }
})
})
})

【问题讨论】:

    标签: javascript node.js firebase google-cloud-functions


    【解决方案1】:

    admin.database().ref('/convoID/' + key).once(...) 是异步的并返回一个承诺,但您的代码没有使用这些承诺来等待每个查询完成。这意味着您的代码将在快照迭代之后立即移动到 if/else,但在更新计数器或添加批次之前。

    您需要重组代码以将所有这些承诺从 once() 收集到一个数组中,使用 Promise.all() 等待所有这些承诺解决,然后提交批处理。

    【讨论】:

    • 感谢您的快速响应。我将如何收集承诺?我会把它变成 .then(snapshot) 并返回所有批量删除吗? .once('value').then(snapshot => { 之类的东西并与 return [updates] 结合起来?
    • 这个链接非常有帮助;但是,我尝试在我的答案更新中实现它,我现在收到Each promise should return a valueExpected catch or returnAvoid nesting promises 我创建了一系列承诺;但是,我需要退回它们。我如何在从return getPostsForDate.get().then(snapshot => { 返回的同时返回它们
    • 抱歉,该评论可能过于宽泛。相反,我需要为对话或每个文档使用 promise.all 吗?如您所见,我做snapshot.forEach(doc) AND for (var child in convoIDCollection) {
    • 我不确定它对后者有什么帮助,因为它没有做异步工作。
    猜你喜欢
    • 1970-01-01
    • 2019-02-28
    • 2021-06-19
    • 2020-07-02
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    相关资源
    最近更新 更多