Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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);
          });
       });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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;
          });
       });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!