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

264
Vistas
event.data.ref in a firebase function for realtime database is unexpectedly null

Do i have a syntax error or did the the function change from when what i am currently reading was written.

cloud function:

exports.onUserStatusChanged = functions.database
  .ref("/status/{userId}") // Reference to the Firebase RealTime database key
  .onUpdate((event: EventInterface) => {
    onUserStatusChanged(event, admin);
  });



function onUserStatusChanged(event, admin) {
  const usersRef = admin.firestore().collection("users"); // Create a reference to the Firestore Collection
  const scheduledRef = admin.firestore().collection("scheduled");

  return event.data.ref
    .once("value")
    .then((statusSnapshot) => {
      console.log("statusSnapshot - ", statusSnapshot);
      statusSnapshot.val();
    }) // Get the latest value from the Firebase Realtime database
    .then((status) => {
      // check if the value is 'offline'
      if (status === "offline") {
        // Set the Firestore's document's online value to false
        usersRef.doc(event.params.userId).update({ online: false });
        scheduledRef.add({
      type: "offline",
      user: event.params.userId,
      time: new Date().getTime(),
    });
      }
    });
}

EventInterface:

interface EventInterface {
  data: { ref: string };
  params: { userId: string };
}

logs me the error: " TypeError: Cannot read properties of undefined (reading 'ref') at onUserStatusChanged (/workspace/triggerFuncs.js:5:21) "

the actual onDisconnect hook in the realTime db does work. marks me at offline and online without issues. What am I missing?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're not returning anything from the top-level export. While this may not explain the error message you posted, it does mean the function will likely get interrupted before its asynchronous database interactions complete.

To fix this:

exports.onUserStatusChanged = functions.database
  .ref("/status/{userId}") // Reference to the Firebase RealTime database key
  .onUpdate((event: EventInterface) => {
    return onUserStatusChanged(event, admin);
    // 👆
  });
about 4 years ago · Juan Pablo Isaza 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