无法将Firebase功能拆分为多个文件 [英] unable to split Firebase functions in multiple files

查看:77
本文介绍了无法将Firebase功能拆分为多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase函数,并且已经使用了数百个函数,现在很难像单个例子中的许多示例所示那样在单个index.js文件中对其进行管理

I'm working with firebase functions and arrived to hundreds of functions, and now it is very hard to manage it in single index.js file as shown in their lots of examples

我试图将该功能拆分为多个文件,例如:

I tried to split that functions in multiple files like:

--firebase.json
--functions
  --node_modules
  --index.js
  --package.json
  --app
    --groupFunctions.js
    --authFunctions.js
    --storageFunctions.js

在这种结构中,我将函数分为三类,并放入了三个文件groupFunctions.jsauthFunctions.jsstorageFunctions.js.然后,我尝试在index.js中导入此文件,但是我不知道为什么它对我不起作用.

In this structure i divide my functions in three categories and put in that three files groupFunctions.js, authFunctions.js, and storageFunctions.js. I then tried to import thise files in index.js, but I don't know why it is not working for me.

这是groupFunctions.js

var functions = require('firebase-functions');
module.exports = function(){
    exports.onGroupCreate = functions.database.ref('/groups/{groupId}')
        .onWrite(event => {
            console.log(`A group is created in database named:${event.params.groupId}.`);
            // some logic...
            //...
        })
}

这是index.js文件:

var functions = require('firebase-functions');
module.exports = require("./app/groupFunctions")();

我的编辑器未在此代码中给出任何警告.但是,当我使用firebase deploy --only functions部署此代码时,它不会部署功能.如果Firebase控制台上已经存在某些功能,则会删除部署时的所有功能.

My editor not giving any warning in this code. But when I deploy this code with firebase deploy --only functions, it does not deploy function. If some functions already exist on firebase console, it remove all functions on deploy.

这是部署日志:

问题

推荐答案

工作代码示例:

文件结构:

--firebase.json
--functions
  --node_modules
  --index.js
  --package.json
  --src
    --groupFunctions.js
    --authFunctions.js
    --storageFunctions.js

index.js文件:

require('./src/groupFunctions.js')(exports);
require('./src/authFunctions.js')(exports);
require('./src/storageFunctions.js')(exports);

groupFunctions.js文件:

var functions = require('firebase-functions');

module.exports = function (e) {
    e.onGroupCreate = functions.database.ref('/groups/{groupId}')
        .onWrite(event => {
            console.log(`A group is created in database named:${event.params.groupId}.`);
            // some logic...
            //...
        })
}

更新:现在我有更好的解决方案

完整的工作代码位于 https://github.com/malikasinger1/firebase -functions-with-typescript ,并使用诸如打字稿和Webpack之类的尖端技术编写.您可以将其用作样板/启动器.

UPDATE: now I have better solution

The full working code is located at https://github.com/malikasinger1/firebase-functions-with-typescript and it's written with cutting edge tech like typescript and webpack. You may use this as a boilerplate/starter.

这篇关于无法将Firebase功能拆分为多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆