【发布时间】:2017-05-14 04:30:11
【问题描述】:
我正在尝试从 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 的响应?
【问题讨论】:
标签: javascript node.js firebase google-cloud-functions