【问题标题】:Is return value important in Firebase Cloud FunctionsFirebase Cloud Functions 中的返回值是否重要
【发布时间】:2021-07-10 16:24:47
【问题描述】:

我正在使用 TypeScript 编写 Firebase 可以函数,以下是更新文档的简单方法。

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);

export const handleTestData = functions.firestore.document('test/{docID}').onCreate(async (snap, context) => {
    const data = snap.data();
    if (data) {
        try {
                await admin.firestore().doc('test1/' + context.params.docID + '/').update({duplicate : true});
            } catch (error) {}
    }
});

在此方法中,promise 由async await 处理,并且没有return 语句,它工作正常。我见过的大多数示例/教程在每种方法中总是有一个return 语句。 我在 Firebase Cloud Functions 中没有返回任何内容是否有任何影响/差异?如果我应该返回一些东西,我可以返回null吗?

【问题讨论】:

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


    【解决方案1】:

    Firebase Cloud Functions 中的返回值重要吗?

    是的,确实是关键,在执行异步处理的云函数(也称为“后台函数”)中,当所有异步处理完成时返回一个 JavaScript 承诺,如中所述documentation

    这样做很重要,主要有两个原因(摘自文档):

    1. 您确保运行您的 Cloud Function 的 Cloud Functions 实例在您的函数成功达到其终止条件或状态之前不会关闭。
    2. 您可以避免从运行时间过长或无限循环的 Cloud Functions 收取过多费用。

    为什么即使你没有返回 Promise,你的 Cloud Function 也能正常运行?

    通常,您的 Cloud Functions 应该在异步操作完成之前终止,因为您不返回 Promise 并因此向 Cloud Functions 平台指示它可以终止运行 Cloud Functions 的 Cloud Functions 实例。

    但有时,Cloud Functions 平台不会立即终止 Function,异步操作可以完成。这根本无法保证并且完全不受您的控制。

    经验表明,对于短的异步操作,最后一种情况经常发生,开发人员认为一切正常。但是,突然之间,有一天,云函数不起作用……有时它确实起作用了:开发人员面临着一种没有任何明确逻辑的“不稳定”行为,这使得调试变得非常困难。您会在 Stack Overflow 中找到很多问题来说明这种情况。


    因此,具体而言,在您的情况下,您可以像这样调整您的代码:

    export const handleTestData = functions.firestore.document('test/{docID}').onCreate(async (snap, context) => {
        const data = snap.data();
        if (data) {
            try {   
                    // See the return below: we return the Promise returned by update()
                    return admin.firestore().doc('test1/' + context.params.docID + '/').update({duplicate : true});
             } catch (error) {
                   return null;  // <- See the return
            }
        } else {
           return null;  // <- See the return
        }
    });
    

    或喜欢

    export const handleTestData = functions.firestore.document('test/{docID}').onCreate(async (snap, context) => {
        const data = snap.data();
        if (data) {
            try {
                    await admin.firestore().doc('test1/' + context.params.docID + '/').update({duplicate : true});
                    return null;  // <- See the return
             } catch (error) {
                   return null;  // <- See the return
            }
        } else {
           return null;  // <- See the return
        }
    });
    

    返回null(或true,或1...)是有效的,因为async 函数总是返回一个Promise。

    【讨论】:

    • 嗨@Renaud,非常感谢您的解释。一个问题。我有一些更大的功能,有很多分支来结束功能。我可以在函数结束时只返回一次 null 而不是在所有分支中返回它吗?例如,您在示例代码的 3 个位置返回 null。这是否与在函数末尾返回 null 相同。因为如果if(data) 失败,那也应该返回一个承诺。
    • 是的,因为您使用的是async/await,您可以编写类似于同步代码的代码。如果您使用then() 而不是async/await,则必须注意在异步工作完成之前不要调用return
    猜你喜欢
    • 2019-03-02
    • 2017-12-01
    • 1970-01-01
    • 2021-09-03
    • 2018-09-22
    • 1970-01-01
    • 2018-12-04
    • 2017-10-19
    • 1970-01-01
    相关资源
    最近更新 更多