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?
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);
// 👆
});