Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 函数返回了未定义的预期承诺或值。虽然我正在返回snapshot.val();_Android_Firebase_Google Cloud Functions - Fatal编程技术网

Android 函数返回了未定义的预期承诺或值。虽然我正在返回snapshot.val();

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;

我正在为我的项目编写一个云函数。在过去的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);

         });    

   });

你需要回报你的承诺

 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());