I am trying to run a function in asynchronously.But it throws error. Below is my code:
exports.listenForNotificationRequests = functions.database.ref('/notificationRequests/{pushId}')
.onWrite(event => {
const requestId = event.data.val();
var sentTo = requestId.sentTo;
var senderIds = sentTo.split(",");
// var senderTokens = "";
var payload = {
data: {
title: requestId.username,
message: requestId.message,
sentFrom: requestId.sentFrom
}
};
return getSenderIds(senderIds).then(function(senderTokens){
console.log("SenderTokens", senderTokens);
admin.messaging().sendToDevice(senderTokens.split(","), payload)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
});
});
function getSenderIds(senderIds){
var senderTokens = "";
senderIds.forEach(function (snapshot){
var ref = admin.database().ref("users/"+snapshot+"/token");
console.log("refernce", snapshot);
ref.once("value", function(querySnapshot2) {
var snapVal = querySnapshot2.val();
console.log("Token", snapVal);
senderTokens = senderTokens+","+snapVal;
});
});
return senderTokens;
}
While on execution it throws exceprtion:
TypeError: getSenderIds(...).then is not a function
at exports.listenForNotificationRequests.functions.database.ref.onWrite.event (/user_code/index.js:20:39)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:35:20
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
I have tried multiple solutions but none of use. Can anybody here point out what mistake I am making? Or if there is any other solution to this?
via Saira Nawaz
No comments:
Post a Comment