I have written a firebase function to send notification whenever a like occurs in my android app. The notification functionality works good most of the times but sometimes does not work.
I receive this error always ( whether it is working or not):
Function returned undefined, expected Promise or value
Here's the code of my like function:
exports.sendactorLikeNotification = functions.database.ref('/Likes/{post_id}/{user_id}')
.onWrite(event => {
if (event.data.exists())
{
const message = event.data.val();
const userUid = event.params.user_id;
const ownerUid = message.owner_id;
console.log("Owner id", ownerUid);
const userPic = message.thumb_image;
const userName = message.name;
const post_key = event.params.post_id;
const timestamp = admin.database.ServerValue.TIMESTAMP;
if(ownerUid == userUid){return null;}
const Promise1= admin.database().ref(`/notifs/${ownerUid}`).push({
thumb_image: userPic,
name: userName,
user_id: userUid,
post_id: post_key,
text: "liked your post",
type: "Like",
read: "false",
time: timestamp
});
const Promise2=admin.database().ref(`/Users/${ownerUid}/device_token`).once('value');
const Promise3= Promise2.then(function(snapshot) {
const getrealDeviceTokensPromise = snapshot.val();
console.log("Device Token", getrealDeviceTokensPromise);
// Notification details.
const payload = {
notification: {
title: 'Appname',
body: userName + ' has liked your post.',
icon: "default",
sound: "default",
click_action: "OPEN_ACTIVITY_1"
}
};
const Promise4= admin.messaging().sendToDevice(getrealDeviceTokensPromise, payload)
.then(function (response) {
console.log("Successfully sent message:", response);
return Promise.all([Promise1,Promise3,Promise4]);
})
.catch(function (error) {
console.log("Error sending message:", error);
return null;
});
}, function(error) {
// The Promise was rejected.
console.error(error);
return null;
});
}
else
{
return null;
}
});
I don't understand where I am going wrong. Please help!