【问题标题】:what should I do If I want to do nothing in the one of my execution path in Background trigger cloud function?如果我想在后台触发云功能的执行路径之一中什么都不做,我该怎么办?
【发布时间】:2018-09-07 10:30:03
【问题描述】:

据我所知,后台触发云函数应该返回一个承诺,对吧?但是如果我不想在我的执行路径之一中做任何事情呢?

export const updateDataWhenUserUnattendTheEvent = functions.firestore
    .document('events/{eventId}/Attendee/{userId}')
    .onDelete((snap, context) => {

        const eventID = context.params.eventId
        const eventRef = snap.ref.firestore.collection('events').doc(eventID)
        const db = admin.firestore()

        return db.runTransaction(async t => {
            const doc = await t.get(eventRef)

            if (doc) {

                const eventRankPoint = doc.data().rankPoint
                let eventCapacity = doc.data().capacity 

                return t.update(eventRef,{
                    isFullCapacity : false,
                    capacity : eventCapacity + 1,
                    rankPoint: eventRankPoint - 1
                })

            } else {

                // what should I write in here? empty promise?


                return new Promise()

            }




        })


    })

我希望我的功能仅在文档存在时才起作用。所以我该怎么做 ?我写了新的 Promise 但是....我不知道该怎么做。提前致谢

【问题讨论】:

    标签: firebase google-cloud-functions


    【解决方案1】:

    如果在您的函数的某些代码路径中没有要执行的异步工作,您可以返回 null。只有当它跟踪一些异步工作时,你才真正需要一个 Promise。

    或者,您可以返回一个立即通过 Promise.resolve(null) 解决的承诺

    【讨论】:

    【解决方案2】:

    因为db.runTransaction 是一个async 函数,所以它会一直返回一个Promise

    您可以删除 else 语句,该方法将按预期执行,因为 runTransaction 将返回 Promise<void>,这是 Cloud Functions 的有效响应

    export const updateDataWhenUserUnattendTheEvent = functions.firestore
        .document('events/{eventId}/Attendee/{userId}')
        .onDelete((snap, context) => {
            const eventID = context.params.eventId;
            const eventRef = snap.ref.firestore.collection('events').doc(eventID);
            const db = admin.firestore();
    
            return db.runTransaction(async t => {
                const doc = await t.get(eventRef);
    
                if (doc) {
                    const eventRankPoint = doc.data().rankPoint;
                    let eventCapacity = doc.data().capacity ;
    
                    return t.update(eventRef,{
                        isFullCapacity : false,
                        capacity : eventCapacity + 1,
                        rankPoint: eventRankPoint - 1
                    });
                }
            });
        });
    

    您还可以将onDelete 函数设为async,这意味着您可以强制它始终返回Promise - 以下是有效的并且将正确退出该函数。

      export const updateDataWhenUserUnattendTheEvent = functions.firestore
        .document('events/{eventId}/Attendee/{userId}')
        .onDelete(async (snap, context) => {
            // Do Nothing
    
            return;
        });
    

    【讨论】:

      猜你喜欢
      • 2011-02-14
      • 1970-01-01
      • 2019-12-05
      • 2019-10-03
      • 2022-11-12
      • 2022-12-05
      • 2021-10-05
      • 1970-01-01
      • 2021-01-21
      相关资源
      最近更新 更多