我试图在 firebase 更新后返回数据,但它显示为 undefined
test().then((res) => {
alert(res);
});
test() {
var fredNameRef = new Firebase('https://docs-examples.firebaseio.com/samplechat/users/fred/name');
var onComplete = function(error) {
if (error) {
return error;
} else {
return "success";
}
};
fredNameRef.update({ first: 'Wilma', last: 'Flintstone' }, onComplete);
}
最佳答案
onComplete
callback用于检查更新过程中的错误,而不是接收数据。
Firebase SDK surfaces update first locally ,然后如果成功,它会触发 onComplete
回调。
在您的情况下,您希望保存数据,然后使用 .on()
方法监听更新。
var fredNameRef = new Firebase('https://docs-examples.firebaseio.com/samplechat/users/fred/name');
fredNameRef.on('value', function(snap) {
console.log(snap.val());
});
fredNameRef.update({ first: 'Wilma', last: 'Flintstone' });
.update()
方法执行后,会触发.on()
方法。
关于javascript - 如何在 firebase 更新后返回成功数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36105690/