我正在尝试编写一个执行以下操作的云函数:
下面的代码工作正常,直到最后一部分尝试完成工作。3. 错误消息是“函数返回未定义,预期的承诺或值”。
我认为这是 Firebase 特有的语法问题。有人可以指出我用来完成任务的正确语法吗?
非常感谢!
exports.commentsupdate = functions.database.ref('posts/{postid}/comments/{commentsid}/')
.onCreate((snapshot,context) => {
const uid=snapshot.val()['uid'];
let username="";
let nickname="";
let profile_picture="";
const ref1=database.ref('users/'+uid);
ref1.once('value',function(ssnapshot){
username=ssnapshot.val()['username'];
nickname=ssnapshot.val()['nickname'];
profile_picture=ssnapshot.val()['profile_picture'];
}).then(()=>{
return snapshot.ref.update({
username:username,
nickname:nickname,
profile_picture:profile_picture
})
})
});
需要返回Promises链中的第一个Promise ,即once()
方法返回的Promise ,如下:
exports.commentsupdate = functions.database.ref('posts/{postid}/comments/{commentsid}/')
.onCreate((snapshot, context) => {
const uid = snapshot.val()['uid'];
let username = "";
let nickname = "";
let profile_picture = "";
const ref1 = database.ref('users/' + uid);
return ref1.once('value') // <--- Note the return here
.then(snapshot => {
username = snapshot.val()['username'];
nickname = snapshot.val()['nickname'];
profile_picture = snapshot.val()['profile_picture'];
return snapshot.ref.update({
username: username,
nickname: nickname,
profile_picture: profile_picture
})
})
});
我建议您观看 Firebase 视频系列中关于“JavaScript Promises”的 3 个视频:https ://firebase.google.com/docs/functions/video-series/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句