简体   繁体   English

Firebase Cloud Functions 中的可重用函数

[英]Reusable functions in Firebase Cloud Functions

My problem is trying to define a local function in Firebase Cloud Functions.我的问题是尝试在 Firebase Cloud Functions 中定义一个本地函数。 I would like my function to be global and reusable, but it seems to not export when I deploy using Firebase-CLI.我希望我的函数是全局的和可重用的,但是当我使用 Firebase-CLI 部署时它似乎无法导出。

function mapEvents(data) {
    // Very long calculation
    ...
    return events
}

exports.importEvents = functions.https.onRequest((req, res) => {
    ...
    const mappedEvents = mapEvents(data);
    ...
})

This is the logged error I'm getting in the Firebase console when I run importEvents:这是我在运行 importEvents 时在 Firebase 控制台中遇到的记录错误:

TypeError: this.mapEvent is not a function at module.exports.importEvents.functions.https.onRequest (/user_code/index.js:199:29) at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:26:41) TypeError: this.mapEvent 不是 cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https) 的 module.exports.importEvents.functions.https.onRequest (/user_code/index.js:199:29) 的函数.js:26:41)

This might be late but helpful for someone else.这可能晚了,但对其他人有帮助。

Since you are using arrow function on exports.importEvents由于您在exports.importEvents上使用箭头函数

Update the mapEvents function to arrow function将 mapEvents 函数更新为箭头函数

const mapEvents = (data) => {
    // Very long calculation
    ...
    return events
}

Hope this helps希望这有帮助

You can read more about Arrow Functions here您可以在此处阅读有关箭头函数的更多信息

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM