【问题标题】:Firebase Cloud Messaging Update Listener Function throws Function returned undefined, expected Promise or valueFirebase 云消息传递更新侦听器函数抛出函数返回未定义、预期的 Promise 或值
【发布时间】:2019-08-07 20:08:30
【问题描述】:

我在节点的特定值更新时编写了firebase云函数,然后需要触发它。firebase结构和代码如下。我为此使用javascript firebase cli。东西在firebase控制台中它不断抛出Function returned undefined, expected Promise or value

Node name/Id
    |--sensore1:10;
    |--sensore2:20;
    |--sensore3:50;


 exports.pressureExceeding = functions.database.ref("Reservoir/{id}")
.onUpdate(evnt => {
  console.log(evnt.after.val);
  const sensData = evnt.after.val;
  const status = sensData.sensor3;
  console.log(evnt.after.val);
  if (status > 71) {
    const payLoad = {
      notification: {
       title: "Emergency Alert",
       body: "{sensData.keys} Pressure is High",
       badge: "1",
       sound: "defualt"
      }
  };
  admin.database().ref("FcmToken").once("value")
     .then(allToken => {
       if (allToken.val()) {
         console.log("token available");
         const token = Object.keys(allToken.val());
         return admin.messaging().sendToDevice(token, payLoad);
       } else {
         console.log("no token available");
       }
     });
   }
});

【问题讨论】:

    标签: firebase firebase-cloud-messaging google-cloud-functions


    【解决方案1】:

    1/ 您没有正确返回 once() 异步方法返回的 Promise。

    2/下面几行也有错误:

      console.log(evnt.after.val);
      const sensData = evnt.after.val;
    

    应该是:

      console.log(evnt.after.val());
      const sensData = evnt.after.val();
    

    因为val() 是一种方法

    3/ 最后你应该考虑status

    因此,您应该如下调整您的代码:

     exports.pressureExceeding = functions.database.ref("Reservoir/{id}")
    .onUpdate(evnt => {
      console.log(evnt.after.val);
      const sensData = evnt.after.val;
      const status = sensData.sensor3;
      console.log(evnt.after.val);
      if (status > 71) {
        const payLoad = {
          notification: {
           title: "Emergency Alert",
           body: "{sensData.keys} Pressure is High",
           badge: "1",
           sound: "defualt"  // <- Typo
          }
      };
    
      //What happens if status <= 71?? You should manage this case, as you are using payload below.
    
      return admin.database().ref("FcmToken").once("value"). // <- Here return the promise returned by the once() method, then you chain the promises
         .then(allToken => {
           if (allToken.val()) {
             console.log("token available");
             const token = Object.keys(allToken.val());
             return admin.messaging().sendToDevice(token, payLoad);
           } else {
             console.log("no token available");
             return null;   // <- Here return a value
           }
         });
       }
    });
    

    最后一点:您使用的是旧语法,版本 https://firebase.google.com/docs/functions/beta-v1-diff

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 2018-06-12
    • 2019-08-06
    • 1970-01-01
    • 2018-12-03
    • 2019-11-03
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    相关资源
    最近更新 更多