我试图从Firebase设置一些变量,然后将它们传递给另一个函数 . 目前,Promise.all正在正确设置foo和bar,但在 .then
期间会抛出错误,因此响应未在Firebase中设置 .
const functions = require('firebase-functions'),
admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.someFunc = functions.database.ref(`/data`).onWrite(event => {
const condition = event.data.val()
if (condition) {
// Get the data at this location only once, returns a promise, to ensure retrieval of foo and bar
const foo = event.data.adminRef.child('foo').once('value')
const bar = event.data.adminRef.child('bar').once('value')
return Promise.all([foo, bar]).then(results => {
const foo = results[0].val()
const bar = results[1].val()
return someModule.anotherFunction({
"foo": foo,
"bar": bar
}).then(response => {
// Get an error thrown that Firebase is not defined
let updates = {}
updates['/data'] = response
return firebase.database().ref().update(updates)
})
})
} else {
console.log('Fail')
}
});
登录到控制台的错误如下:
ReferenceError: firebase is not defined
at someModule.anotherFunction.then.response
(/user_code/index.js:100:16)
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
return firebase.database().ref().update(updates);
如何设置范围来设置 anotherFunction
的响应?
1 回答
您需要include the Firebase Admin SDK,通常称为
admin
.如_591698中所示和解释:
const admin = require('firebase-admin');
admin.initializeApp(functions.config()火力);
这些行加载firebase-functions和firebase-admin模块,并初始化可以从中进行实时数据库更改的管理应用程序实例 .
然后你像这样使用它:
另见complete example in the functions-samples repo .