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

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

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 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