I am trying to write a cloud function that does the following:
- Listen to a new creation in 'posts/{postid}/comments/{commentsid}/' node. (This is done from database push from the frontend code). This node will have the commenter's uid under 'uid' child node.
- Using the uid in the child node, Look for the commenter's username, nickname, and profile picture in the 'users/uid' node and log them.
- Update the 'posts/{postid}/comments/{commentsid}/' node with the relevant child nodes for username, nickname, and profile picture.
The code below works fine until the last part that try to do the job no. 3. The error message is 'Function returned undefined, expected Promise or value'.
I think this is a syntax problem specific to Firebase. Could someone point me to the right syntax to use to do the task?
Thanks a lot!
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
})
})
});