我想将大型index.js 文件拆分为多个文件。为了组织代码,我可以在多个 js 文件中编写 firebase 触发器吗?如果是这样,请帮助我如何正确写作
child.js
exports.testFunction = functions.database.ref(`/test/`).onWrite((snap, context) => {
console.log(snap.val());
return null;
});
...
index.js
const childFunction = require('./child.js');
...
最佳答案
基本上,您需要做的是将函数分离在不同的文件中,并将它们导入到您的 index.js 中,如下所示:
1.) In the .js file to be imported such as ‘modA.js’
module.exports = { func1: function () { // function 1 }, func2: function () { // function 2 } };
2.) Import it in your index.js:
var moduleA = require('./modA');
3.) Run it like:
moduleA.func1();
关于javascript - 我可以在其他文件而不是index.js中编写firebase实时数据库触发器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58127978/