I'm trying to send a notification to a user but for some reason i have an error. The error is "Function returned undefined, expected Promise or value".
I cant understand what does this line means, hope you guys can take a look at my code and tell me where im wrong.
CODE
exports.sendNotification = functions.database.ref('/likes/{videoId}/{currentUser}').onWrite(event =>{
console.log("Start send like notification");
const model = event.data.val();
let ownerVideoUserId = model.userVideoID;
let username = model.userName;
if(username == ""){
username = "Somebody";
}
console.log("model notifi: ",model);
console.log("userVideoID notifi: ",ownerVideoUserId);
console.log("username notifi: ",username);
let reference = admin.database().ref("/users/" + ownerVideoUserId);
reference.once('value').then(snap => {
//get the token
let token = snap.child('token').val();
console.log("user token: ",token);
const payload = {
data: {
title: "Talent",
message: username + " liked your video",
}
}
return admin.messaging().sendToDevice(token,payload).then(function(res){
console.log("Successfully sent message:", res);
}).catch(function(error) {
console.log("Error sending message:", error);
});
})
});
Can you spot my mistake?
EDIT exports.sendCommentNotification = functions.database.ref('genresComments/{genre}/{videoId}/{userId}').onWrite(event =>{ console.log("Start send comment notification"); const model = event.data.val(); let ownerVideoUserId = model.ownerVideoID; let username = model.userName;
if(username == "")
username = "Somebody";
console.log("model: ",model);
console.log("userVideoID: ",ownerVideoUserId);
console.log("username: ",username);
let reference = admin.database().ref("/users/" + ownerVideoUserId);
return reference.once('value').then(snap => {
//get the token
let token = snap.child('token').val();
console.log("user token: ",token);
const payload = {
data: {
title: "Talent",
message: username + " comment your post",
}
}
return admin.messaging().sendToDevice(token,payload).then(function(res){
console.log("Successfully sent message:", res);
}).catch(function(error) {
console.log("Error sending message:", error);
});
}).catch(function(error){
console.log("Error with the reference:",error);
}).then(()=>{
console.log('notification');
});
});