Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!