0

I want to update CategoryBalance of a Item 2. Being very new to Firebase and React Native, I am finding difficulty in accessing the CategoryBalance key.

sample_json

I am using firebase.push() to add the following and I am not storing any keys.

{
    CategoryName: "Item 4",
    CategoryBalance: 0
}

If I use the following code, it is adding a new values (red box in the image). But I need to update in the node (green box in the image)

db.ref().update({
        "CategoryBalance": 10,
        "CategoryName": "Food Card 2"
    })

JSON_AFTER_UPDATE

I am posting this question after checking many examples and questions over internet. I couldn't find any solution. Any help on the code snippet is highly appreciated. Thanks in advance

Sravan
  • 1,891
  • 4
  • 23
  • 28
  • You're going to have to identify the specific node to update using its unique ID that was generated by push(). You don't really have any alternatives to this. – Doug Stevenson Jul 19 '20 at 18:25
  • @DougStevenson, any suggestions/tutorial/working solution on identifying and updating the values of the node? – Sravan Jul 19 '20 at 18:28
  • 1
    I'm not sure how a tutorial would help you. You just need to know that ID. Or you can make a query to find the ID, based on what know about its child values. – Doug Stevenson Jul 19 '20 at 18:32
  • 1
    You'll need to know something about the node(s) that you want to update. If you know the key, you can update the node directly by specifying its entire path. If you don't know the key, but know for example the `CategoryName`, you can perform a query to find the matching key(s). See https://stackoverflow.com/questions/45874969/firebase-grab-by-orderbychild-then-update-results-key/45875102#45875102, https://stackoverflow.com/a/58525036, https://stackoverflow.com/a/49719026 – Frank van Puffelen Jul 19 '20 at 20:11
  • Thanks @FrankvanPuffelen. It worked. Your references helped me. – Sravan Jul 20 '20 at 13:26

0 Answers0