Home > OS >  Function finished with status: 'ok' but console logs shows Function returned undefined, ex
Function finished with status: 'ok' but console logs shows Function returned undefined, ex

Time:12-04

I am trying to send notifications using firebase cloud functions but when I update the field it shows Function returned undefined, expected Promise or value first, and then Function execution took 364 ms, finished with status: 'ok'.

My code

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

CodePudding user response:

You are missing a return on the db.collection("Tokens").doc(notificationFor).get() line

also you should return null and make sure you pass a promise or value

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;
          });
       });
  • Related