我正在为我的项目编写一个云函数。在过去的12个小时里,我一直被这个错误所困扰"函数返回了未定义的,预期的Promise或value"我已经尝试了很多次删除它,但无法找到如何解决它
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);
});
});
当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()
});
})
您需要返回promise
return query.once('value').then(snapshot=>{
//....
不返回snapshot.val((值,而是返回Promise
return Promise.resolve(snapshot.val());