Android 函数返回了未定义的预期承诺或值。虽然我正在返回snapshot.val();
我正在为我的项目编写一个云函数。在过去的12个小时里,我遇到了这个错误“函数返回了未定义的、预期的承诺或值” 我已经试了很多次去移除它,但是没有找到解决方法Android 函数返回了未定义的预期承诺或值。虽然我正在返回snapshot.val();,android,firebase,google-cloud-functions,Android,Firebase,Google Cloud Functions,我正在为我的项目编写一个云函数。在过去的12个小时里,我遇到了这个错误“函数返回了未定义的、预期的承诺或值” 我已经试了很多次去移除它,但是没有找到解决方法 exports.Notify = functions.database.ref('blood_share/Notifications/{user}/{notificationid}').onWrite((change, context) => { const username=context.params.user;
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);
});
});
你需要回报你的承诺
return query.once('value').then(snapshot=>{
//....
当
查询。once('value')
解析时,您将返回该值
要澄清这一点,请看以下内容:
let a = 0;
asyncFunctionPromise.then(() => {
a = 1;
});
console.log(a); // Will print 0 instead of
而是直接返回promise返回查询。一次('value')。然后(..
或使用
return new Promise((resolve) => {
query.once('value').then(snapshot=>{
// do something
resolve(snapshot.val()); // To resolve the promise with snapshot.val()
});
})
返回承诺,而不是返回snapshot.val()值
return Promise.resolve(snapshot.val());