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

161
Visualizações
Firebase function error: Cannot convert undefined or null to object at Function.keys (<anonymous>)

Description of the problem:

My App aim is to store family spending in Firebase Realtime Database. I want that, when a new spending is stored, a notification is sent to all other devices. I try to send a notification to a single device and it works fine, but when I try to get all the tokens in an array, I have an error: TypeError: Cannot convert undefined or null to object at Function.keys ().

code of index.js :

const functions = require("firebase-functions");
const admin = require("firebase-admin");
 admin.initializeApp();

exports.androidPushNotification = functions.database
    .ref("{nodo}/spese/{spesaPush}")
    .onCreate(async (snapshot, context) => {
      const original = snapshot.val();

      const getDeviceTokensPromise = admin.database()
          .ref(`/utenti/{tutti}/token`).once('value');

      let tokensSnapshot;
      let tokens;
      tokensSnapshot = await getDeviceTokensPromise;

      // This is the line generating the errors. 
      // If I get only a specific token and 
      // use tokens = tokensSnapshot.val(); anything works fine
      tokens = Object.keys(tokensSnapshot.val());


      const result = original.chi + " ha speso " +
      original.costo + " € per acquistare " +
      original.desc;
      console.log(result);

      const payload = {
        notification: {
          title: 'New spending inserted!',
          body: result, 
        }
      };

      const response = await admin.messaging().sendToDevice(tokens, payload);
      return result;
    });

It seems that values are not reached yet, but I thought that the keyword await lets system to wait until all data are available. From the log I noticed that the value I need is null and I don't understand why. If I use this line of code:

      const getDeviceTokensPromise = admin.database()
          .ref(`/utenti/SpecificUser/token`).once('value');
      ....
      //then
      tokens = tokensSnapshot.val();

The notification is sent to the device that has the token under the name of "SpecificUser"

EDIT: I provide a pic of my db. I notice that none of the field is null, so I don't know why I see this error enter image description here

Thank you to anyone that helps me

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

i had same error and it is solve by database... when i saw my database values unfortunately i stored undefined value so my whole result got error like you... see your whole values and fields that store values properly.

about 4 years ago · Santiago Trujillo 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