【问题标题】:Cloud firebase batch updateCloud Firebase 批量更新
【发布时间】:2020-05-07 17:31:39
【问题描述】:

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

  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";
            })
    })

【问题讨论】:

    标签: firebase promise google-cloud-firestore google-cloud-functions


    【解决方案1】:

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

    以下应该可以解决问题(未经测试):

    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),您可以使用 Promise.all()。在您的情况下(如果我没记错的话),您需要并行执行异步方法的唯一情况是在您使用批量写入的块中,因此并行执行由批量写入本身执行。对于其他方法,它更多是顺序执行,您必须使用then() 方法链接承诺。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-28
      • 2018-08-03
      • 2018-07-04
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 2023-02-23
      • 2022-10-13
      相关资源
      最近更新 更多