Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

309
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda