函数完成状态:'ok' 但控制台日志显示函数返回未定义、预期的 Promise 或值

Function finished with status: 'ok' but console logs shows Function returned undefined, expected Promise or value

我正在尝试使用 firebase 云函数发送通知,但是当我更新字段时,它首先显示函数返回未定义、预期的 Promise 或值,然后函数执行耗时 364 毫秒,完成状态:'ok' .

我的代码

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.updateOrderStatus = functions.firestore
    .document("Users/{userId}/Info_Notifications/{notificationId}")
    .onUpdate((change, context) => {
      const newValue = change.after.data();
      const notificationFor = newValue.orderFrom;
      let deviceID;
      const db = admin.firestore();
      db.collection("Tokens").doc(notificationFor).get()
          .then((doc) => {
            const user = doc.data();
            deviceID = user.id;
            console.log("Device id", deviceID);
            const payload = {
              token: deviceID,
              notification: {
                title: "Test Notification",
                body: "message",
              },
              data: {
                priority: "high",
                timeToLive: "60 * 60* 24",
              },
            };
            console.log("Device ID Out", deviceID);
            admin.messaging().sendToDevice(payload)
                .then((response) =>{
                  return true;
                })
                .catch((error) =>{
                  return console.log("Error Sending Notifications", error);
                });
            return null;
          }).catch((error)=>{
            return console.log("Error in fetching data", error);
          });
       });

您在 db.collection("Tokens").doc(notificationFor).get() 行

中缺少 return

您还应该 return null 并确保您传递了承诺或价值

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.updateOrderStatus = functions.firestore
    .document("Users/{userId}/Info_Notifications/{notificationId}")
    .onUpdate((change, context) => {
      const newValue = change.after.data();
      const notificationFor = newValue.orderFrom;
      let deviceID;
      const db = admin.firestore();
     return db.collection("Tokens").doc(notificationFor).get()
          .then((doc) => {
            const user = doc.data();
            deviceID = user.id;
            console.log("Device id", deviceID);
            const payload = {
              token: deviceID,
              notification: {
                title: "Test Notification",
                body: "message",
              },
              data: {
                priority: "high",
                timeToLive: "60 * 60* 24",
              },
            };
            console.log("Device ID Out", deviceID);
            admin.messaging().sendToDevice(payload)
                .then((response) =>{
                  return true;
                })
                .catch((error) =>{
                  console.log("Error Sending Notifications", error);
                  return false;
                });
            
          }).catch((error)=>{
           
            console.log("Error in fetching data", error);
            return false;
          });
       });