首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >云防火墙批处理更新

云防火墙批处理更新
EN

Stack Overflow用户
提问于 2020-01-21 15:54:04
回答 1查看 277关注 0票数 0

我正在尝试使用批处理更新来更新每个快照中的计数。但似乎这个函数甚至没有运行。我知道这与第二个承诺有关,但我不知道在哪里。

代码语言:javascript
复制
  import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

export const replyCreated = functions
    .firestore
    .document(`/Reply/{replyId}`)
    .onCreate((change: any, context: functions.EventContext) => {
        const promises = [];
        promises.push(admin.firestore().doc(`Challenge/${change.data().challenge_id}`).update({replyCount: admin.firestore.FieldValue.increment(1)}))

        promises.push(admin.firestore()
            .collection(`User`)
            .where('following', 'array-contains', change.data().user_id).get().then((snapshot: any) => {
                if (!snapshot.empty) {
                    const batch = admin.firestore().batch();
                    snapshot.forEach((doc: any) => {
                        const tempObject = doc.data()
                        console.log(`/Subscribed_Challenges/${tempObject.userId}/myChallenges/${change.data().challenge_id}`)
                        const myChallenge = admin.firestore().doc(`/Subscribed_Challenges/${tempObject.userId}/myChallenges/${change.data().challenge_id}`)

                        batch.update(myChallenge, {replyCount: admin.firestore.FieldValue.increment(1)})
                    })

                   return batch.commit().catch((err: any) => {
                        console.log('Batch Error', err)
                    });
                }
                else {
                    return Promise.resolve()
                }
            }))

        return Promise.all(promises)
            .then(() => {
                return "upvote complete";
            })
    })
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-21 16:12:44

如果我正确地理解了您的代码,您不需要使用Promise.all(),但是您需要正确地链接异步修复方法返回的不同承诺。

下面的操作应该可以做到(未经测试):

代码语言:javascript
复制
export const replyCreated = functions
    .firestore
    .document(`/Reply/{replyId}`)
    .onCreate((change: any, context: functions.EventContext) => {

        return admin.firestore().doc(`Challenge/${change.data().challenge_id}`).update({ replyCount: admin.firestore.FieldValue.increment(1) })
            .then(() => {

                return admin.firestore()
                    .collection(`User`)
                    .where('following', 'array-contains', change.data().user_id).get()

            })
            .then((snapshot: any) => {
                if (!snapshot.empty) {
                    const batch = admin.firestore().batch();
                    snapshot.forEach((doc: any) => {
                        const tempObject = doc.data()
                        console.log(`/Subscribed_Challenges/${tempObject.userId}/myChallenges/${change.data().challenge_id}`)
                        const myChallenge = admin.firestore().doc(`/Subscribed_Challenges/${tempObject.userId}/myChallenges/${change.data().challenge_id}`)

                        batch.update(myChallenge, { replyCount: admin.firestore.FieldValue.increment(1) })
                    })

                    return batch.commit()
                }
                else {
                    throw new Error('Snapshot empty')
                }
            })
            .catch((err: any) => {
                console.log('Error', err);
                return null;
            });

    })

如果需要并行执行许多异步方法(这些方法返回承诺),则可以使用Promise.all()。在您的情况下(如果我没有误解),唯一需要并行执行异步方法的情况是在使用批写的块中,因此并行执行是由批写本身执行的。对于其他方法,它更多地是一个顺序执行,您必须用then()方法来链接承诺。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59844763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档