Javascript Firebase函数:函数返回未定义的预期承诺或值
我已经编写了一个firebase函数,每当我的android应用程序中出现类似事件时发送通知。通知功能在大多数情况下运行良好,但有时不起作用 我总是收到这个错误(无论它是否工作): 下面是我的like函数的代码:Javascript Firebase函数:函数返回未定义的预期承诺或值,javascript,firebase,firebase-cloud-messaging,google-cloud-functions,Javascript,Firebase,Firebase Cloud Messaging,Google Cloud Functions,我已经编写了一个firebase函数,每当我的android应用程序中出现类似事件时发送通知。通知功能在大多数情况下运行良好,但有时不起作用 我总是收到这个错误(无论它是否工作): 下面是我的like函数的代码: exports.sendactorLikeNotification = functions.database.ref('/Likes/{post_id}/{user_id}') .onWrite(event => { if (event.data.exists())
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;
}
});
我不明白我错在哪里。请帮忙 您在以下情况下返回未定义:
event.data.exists()
返回falseownerUid==userUid
您也没有处理
sendToDevice().then().catch()
返回的承诺。该函数需要等待该工作完成后才能终止。请测试以下更改并通知我,同时我建议更新firebase functions SDK:
exports.sendactorLikeNotification = functions.database.ref('/Likes/{post_id}/{user_id}')
.onWrite(event => {
if (event.data.exists()) {
const promises = [];
const message = event.data.val();
const userUid = event.params.user_id;
const ownerUid = message.owner_id;
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;
return Promise.all([admin.database().ref(`/Users/${ownerUid}/device_token`).once('value')]).then(r => {
const cO = r[0];
const aP = 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
});
promises.push(aP);
const payload = {
notification: {
title: 'Appname',
body: userName + ' has liked your post.',
icon: "default",
sound: "default",
click_action: "OPEN_ACTIVITY_1"
}
};
const tokensList = Object.keys(cO.val());
promises.push(admin.messaging().sendToDevice(tokensList, payload));
return Promise.all(promises);
});
}
return null;
});
这个代码为我解决了这个问题 一些观察结果。1) 您的代码很难遵循此处的格式。2)你正在使用一个非常旧版本的FialBasic函数SDK——考虑升级到最新的1。x版本,并将你的代码迁移到新API。嘿,抱歉破旧的格式化。我在编辑中对它进行了改进。我认为你没有所有的代码。上次你打电话来答应我。好的,我错过了。下面是全部代码。请查看itIf event.data.exists()返回false或ownerUid==userUid,则不会发送通知。这里的问题是发送通知,然后我得到返回未定义的错误函数,预期的承诺或值。我已经编辑了问题中的代码,现在也包含了一个Promise4,它负责sendToDevice()。然后(),它仍然不起作用。但是在这种情况下,您的函数将返回未定义的,您将看到您抱怨的消息。返回null或一个承诺。关键是,您需要确保在代码的每个分支中都返回了一些内容,而不仅仅是在主要成功案例中。正如您所建议的,我在这两种情况下都返回了null 1)event.data.exists()返回false,2)ownerUid==userUid。我仍然得到同样的错误。我的第一个评论是强调这样一个事实:当我测试代码时,我确保成功案例发生,而成功案例反过来又给了我前面提到的错误。我也编辑了问题中的代码以反映更改。请调查一下。你的回答帮助我得出了正确的答案。我不知道promises.push功能。函数返回一个承诺,错误消失,但通知不会发送。无论如何,谢谢。太好了,如果设备_令牌是一个只有一个令牌的字符串,那么使用:
promises.push(admin.messaging().sendToDevice(cO.val(),payload))代码>
exports.sendactorLikeNotification = functions.database.ref('/Likes/{post_id}/{user_id}')
.onWrite(event => {
if (event.data.exists()) {
const promises = [];
const message = event.data.val();
const userUid = event.params.user_id;
const ownerUid = message.owner_id;
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;
return Promise.all([admin.database().ref(`/Users/${ownerUid}/device_token`).once('value')]).then(r => {
const cO = r[0];
const aP = 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
});
promises.push(aP);
const payload = {
notification: {
title: 'Appname',
body: userName + ' has liked your post.',
icon: "default",
sound: "default",
click_action: "OPEN_ACTIVITY_1"
}
};
const tokensList = Object.keys(cO.val());
promises.push(admin.messaging().sendToDevice(tokensList, payload));
return Promise.all(promises);
});
}
return null;
});
exports.sendactorLikeNotification = functions.database.ref('/Likes/{post_id}/{user_id}').onWrite(event => {
if (event.data.exists()) {
const promises=[];
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 a1=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
});
promises.push(a1);
const a2= admin.database().ref(`/Users/${ownerUid}/device_token`).once('value').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 a3=admin.messaging().sendToDevice(getrealDeviceTokensPromise, payload)
.then(function (response) {
console.log("Successfully sent message:", response);
})
.catch(function (error) {
console.log("Error sending message:", error);
});
promises.push(a3);
}, function(error) {
console.error(error);
});
promises.push(a1);
return Promise.all(promises);
}
else
{
return null;
}
});