0

So, I have the following firebase structure, whose keys firebase has enumerated, starting at 0. So in the following example the key are numbered 0-3:

"comments" : {
  "BAcyDyQwcXX" : [
    {
      "text" : "Wes. WE should have lunch.",
      "user" :  "jdaveknox"
    },
    {
      "text" : "#adults",
      "user" :  "jdaveknox"
    },
    {
      "text" : "@jdtroy yes!",
      "user" :  "wesbos"
    },
    {
      "text" : " love choccolate!",
      "user" :  "willowtreemegs"
    }
  ]
}

What I want to do is:

  1. get the current record count,
  2. create a new key by incrementing the current record count + 1
  3. write the new details to the child() index of comments

I have the second and third stage of the process completed,

 var commentData = {
   text: text,
   user: user
 };

 updates['/comments/' + childindex +'/' + newCommentKey] = commentData;

 return firebase.database().ref().update(updates);

I just need to know how to get the current record count. How do I do this?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
TheoG
  • 1,498
  • 4
  • 30
  • 54
  • 1
    Arrays in Firebase are almost always a [bad idea.](https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html) Consider [flattening your data](https://www.firebase.com/docs/web/guide/structuring-data.html) and refer to comments by unique keys. – Travis Christian Feb 08 '17 at 22:12
  • There is no operator to return the child node count. See http://stackoverflow.com/questions/15148803/in-firebase-is-there-a-way-to-get-the-number-of-children-of-a-node-without-load (and many of these: http://stackoverflow.com/search?q=%5Bfirebase%5D+children+count). As Travis says, the easiest solution is to not create this problem in the first place and use Firebase's recommend way of storing items in a list: https://firebase.google.com/docs/database/web/lists-of-data – Frank van Puffelen Feb 08 '17 at 23:17

0 Answers0