Functions-samples: "Child count" example, not returning correct count

Created on 12 Mar 2017  路  4Comments  路  Source: firebase/functions-samples

In the Child count example, having copied the DB structure, the following code:

exports.countlikes = functions.database.ref('/posts/{postid}/likes').onWrite(event => { return event.data.ref.parent.child('likes_count').set(event.data.numChildren()); });

likes_count always becomes 0

trying workarounds produces even more irrational results, like the count increases sometimes, sometimes it does not increase, some other times it becomes 0 again.

There's an Stackoverflow question about this:
http://stackoverflow.com/questions/42739299/numchildren-always-returning-0

question

All 4 comments

I also just put together https://github.com/firebase/functions-samples/pull/31 which switches the official sample to properly use transaction().

Awesome Jacob, it works!

FYI that my solution on works if you never do deletion of modifications. For a more robust solution, check out https://github.com/firebase/functions-samples/pull/32 which has already been merged into the main repo. I'll update my Stack Overflow answer.

Was this page helpful?
0 / 5 - 0 ratings