我正在为我的项目编写云功能。这是我被此错误困住的最后12个小时“函数返回了未定义的,预期的承诺或值” 我曾尝试将其删除,但找不到解决方法
exports.Notify = functions.database.ref('blood_share/Notifications/{user}/{notificationid}').onWrite((change, context) => {
const username=context.params.user;
const notifyid=context.params.notificationid;
const query = admin.database().ref("blood_share/Notifications").child(username).child(notifyid);
query.once('value').then(snapshot=>{
const event = snapshot.val();
const notificationMessage = event.messsage;
const token_id=event.reciever_appid;
console.log("message"+notificationMessage);
//create notification Json
const payLoad = {
notification:{
title: "New Request For Food",
body: "kuch b",
icon: "default"
}
};
return snapshot.val();
}).catch(error => {
console.error(error);
});
});
答案 0 :(得分:1)
您需要兑现承诺
return query.once('value').then(snapshot=>{
//....
答案 1 :(得分:1)
您将在query.once('value')
承诺解决时返回值。
为澄清这一点,请看以下内容:
let a = 0;
asyncFunctionPromise.then(() => {
a = 1;
});
console.log(a); // Will print 0 instead of
相反,直接返回承诺return query.once('value').then(....
或使用
return new Promise((resolve) => {
query.once('value').then(snapshot=>{
// do something
resolve(snapshot.val()); // To resolve the promise with snapshot.val()
});
})
答案 2 :(得分:1)
返回Promise而不是返回一个值的snapshot.val()
return Promise.resolve(snapshot.val());