"how to get specific pushedid in firebase?" Code Answer

4

there are two ways in which you can achieve this. so if you want to access a specific comment, you must know something that unique identifies that pcommentll. the first solution would be to store that random id in a variable in the exact moment when you are pushing a new comment to the database using the push() method. to get that id, you can use the following lines of code:

databasereference rootref = firebasedatabase.getinstance().getreference();
string commentid = rootref
    .child(userid)
    .child(photoid)
    .child("comments")
    .push()
    .getkey();

you can also save that id inside the comment object if you need to use it later. so this is how it looks like in code:

map<string, object> map = new hashmap<>();
map.put("nooflikes", 0);
map.put("commentid", commentid);
rootref
    .child(userid)
    .child(photoid)
    .child("comments")
    .child(commentid)
    .updatechildren(map);

once you have this key, you can use it in your reference to update the number of likes. this type of operation is usually done using firebase transaction and for that i recommend see my answer from this post in which i have explained how to update a score property but same principle aplly in the case of likes.

the second approach would be to create an entire new top level collection like this:

firebase-rot
   |
   --- comments
         |
         --- commentid
                |
                --- nooflikes: 1

using this solution it will be more easy for you to query the database becuase to get the number of likes you'll need only this simple reference:

databasereference nooflikesref = rootref
    .child("comments")
    .child(commentid)
    .child("nooflikes");
nooflikesref.addlistenerforsinglevalueevent(/* ... */);
By Divya Sharma on April 19 2022

Answers related to “how to get specific pushedid in firebase?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.