我试图运行云功能并更改数据库上的值,但是每次我返回带有或不带有“ firebase-admin”模块的promise时,该功能都会在60秒后超时。
这是我的代码:
var functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.handleDisconnection = functions.database.ref('/pages/{pageId}/listeners/disconnection')
.onWrite(event => {
const eventSnapshot = event.data;
const isDisconnected = eventSnapshot.val();
const pageId = event.params.pageId;
console.log('there is a new disconnection on page ' + pageId + ', isDisconnected: ' + isDisconnected);
if (eventSnapshot.changed() && isDisconnected) {
console.log('is disconnected true');
return admin.database().ref('/pages/'+pageId+'/listeners/disconnection').set({ disconnection: false }).then(() => {
console.log('Write succeeded!'); // this never triggers
});
}
});
最佳答案
if (eventSnapshot.changed() && isDisconnected) {
console.log('is disconnected true');`
return admin.database.ref('/...')
.set({ disconnection: false }, err => {
if(!err) // No error
{ console.log("Set Updated"); }
});
set方法具有一个将err作为对象传递的回调
您可以使用err来获取操作状态。