0

Following the firebase cloud functions API reference, I am trying to achieve count increase/decrease:

Uploads/
     - Posts/
          - post_1
          - post_2
          ...

     - Likes/
          - post_1/
               - Number: 4
          - post_2/
               - Number: 2
          ...

And,

 exports.LikeCount= functions.database.ref('/Posts/{postID}').onWrite(event => {
    const Ref     = event.data.ref;
    const postID  = event.params.postID; 
    const likeCount= Ref.parent.parent.child('/Likes/' + postID  + '/Number');           

    return likeCount.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("Done");         
    });
});

Other than locations, it's identical to the example given.

It also gives another example where if the number of likes are deleted, then it recalculates the number of likes (children).

Here is my version (or at least the idea of it) where it checks the number of likes and if it is less than 1, then it recalculates it. (Just because the first function will give 1 regardless of the number of the likes present if the number of likes does not exists).

exports.reCount= functions.database.ref('/Likes/{postID}/Number').onUpdate(event => {
    const value = event.data.val;

    //If the value is less than 1: 
    if (value <= 1) {
        const currentRef = event.data.ref;
        const postID     = event.params.postID;             
        const postRef    = currentRef.parent.parent.child('/Uploads/Posts/{postID}/');            

        return postRef.once('value')
            .then(likeData=> currentRef.set(likeData.numChildren()));            
    }
});

With the second function, I tried to get the Number value using the following where event.data.val which gave [Function: val] in the FB logs, where I thought I would get string value.

...and currentRef.parent.parent.child('/Uploads/Posts/{postID}/').numChilren(); gave TypeError: collectionRef.numChildren is not a function.

I read tons of online tutorial and API reference but still bit confused to why I can't get the string value.

I guess I am looking for some examples that I can work from.

ggDeGreat
  • 1,098
  • 1
  • 17
  • 33
Steve Kim
  • 5,293
  • 16
  • 54
  • 99

1 Answers1

5

There's a bunch of things going wrong here.

As you can see from the logs, event.data.val is a function. You'll need to call val() to get the JavaScript object from the location that changed: event.data.val()

Second thing: not sure why you're using currentRef.parent.parent.child when you already know the absolute path of the location to query. You can get there directly with:

currentRef.root.ref(`/Uploads/Posts/${postID}/`)

Third thing, you're using single quotes in what looks like an attempt to use variable interpolation to build this string: /Uploads/Posts/{postID}/. You'll need to use backticks for this, and also use ${} inside there to insert the variable (you're ommitting the $).

Lastly, you should be using a transaction to perform your write, as you see in other sample code, because it's entirely possible for two functions to be running concurrently in an attempt to change the same location. I woudldn't advise leaving that out.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441