我们无法在Firebase云功能中编写多个文件

Mah*_*Gvp 4 firebase google-cloud-functions

我们如何使用Firebase Cloud实现微服务架构我们可以编写多个.js文件,而不像将所有函数写入index.js,这样我们就不需要在单个函数中重新部署所有函数来进行更改

vir*_*966 6

我正在使用firebase函数导入其他.js文件.只需将函数文件夹视为root,我就错误地尝试从/ functions父文件夹导入文件.

index.js

var paymentFunctions = require('./payment_functions');
Run Code Online (Sandbox Code Playgroud)

以及类似的东西:

exports.paymentMethodTask = functions.database.ref('/newPaymentMethodTask/{taskId}').onWrite(event => {
    return paymentFunctions.processPaymentMethodTask(event);
});
Run Code Online (Sandbox Code Playgroud)

使用文件夹结构:

/myProject/functions/index.js
/myProject/functions/payment_functions.js
Run Code Online (Sandbox Code Playgroud)

然后像往常一样在payment_functions.js中导出您的函数:

module.exports = {
    processPaymentMethodTask: function test(event) {
        //do something here with the event
    }
};
Run Code Online (Sandbox Code Playgroud)

https://medium.com/step-up-labs/our-experience-with-cloud-functions-for-firebase-d206448a8850


Fra*_*len 5

您的所有 Cloud Functions for Firebase 都必须在index.js文件中定义。但这并不意味着您必须在单个文件中实现所有功能。

我经常在一个单独的文件中实现每个函数的大部分内容。例如,如果我使用 Google Cloud Vision API 从图像中提取额外的文本,我将有一个ocr.js. 我给这个文件一个主要部分,这样我就可以从本地终端使用node ocr.js. 然后在我的index.js代码中,我只需要导入ocr.js和连接到 Cloud Functions 的代码。

另见: