Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda