1
votes

I'm using Firebase Cloud Functions. I'm trying to count likes on each post. My question is actually in the Cloud functions javascript code. I'm listening to a node in Firebase Databse Realtime that calls "likes". Its Looks like that ->

enter image description here

The Tree is saved like that: PostID -> UserID -> Data

What im trying to do is to get the Data (genre and videoID) in the Cloud Functions.

This is the code

   exports.countlikechange = functions.database.ref('/likes/{postid}/{userUID}').onWrite(event => {
const collectionRef = event.data.ref.parent;
const model = event.data.val();

console.log("model",model);

 const countRef = collectionRef.child('likes');


// Return the promise from countRef.transaction() so our function 
// waits for this async event to complete before it exits.
return countRef.transaction(current => {
  if (event.data.exists() && !event.data.previous.exists()) {
    return (current || 0) + 1;
       }
  else if (!event.data.exists() && event.data.previous.exists()) {
    return (current || 0) - 1;
  }
}).then(() => {
  console.log('Counter updated.');
   });
 });

There is anyway to get the genre and videoId from the node? what should i write? for example: const genre = event.getGenre (something like that)

1

1 Answers

1
votes

The genre and videoID are in the event.data snapshot that is passed to your function. So:

const model = event.data.val();
let genre = model.genre;
let videoID = model.videoID;