Firebase data storing

Hi, im using firebase.
I want that everytime someone hits a button it saves his name on the realtime database on Firebase under a certain key and certain field.

Tom hit the button of the image 1.

Image 1

  • Likes
    — Tom

The problem is when i want to add more people to this list. I tried using a list block, an object block, to save values with variables and databes, but cant make it work.
I would need that if Mark or whoever in the future also hits that button it says:

Image 1

  • Likes
    — Tom
    — Mark

This is the latest i have tried so far, but i have tried many without success.
aiuto_Thunkable

Thanks for your help

for some reason, appending to a list is not so straightforward.

To add to the list, you first need to GET the list, append the item to the list client side, then SAVE the list back to the appropriate key

2 Likes

I’m not sure I fully understand your objective but…
Have you tried playing around with these blocks instead of using the RTDB blocks?

It seems to be working fine for me.

https://x.thunkable.com/projectPage/5fa4d96389dcc300110ba3ea

Add your Firebase credentials in settings and have a look directly at the list being created in the database.

2 Likes

Hi Jared,
you are right, my fault i attached the wrong picture.
aiuto_Thunkable
That was i was doing

Short Answer:
You need to include the array positions in the key of the SAVE block. Something like the example below. Although I listed zeros (0), you will need to use the length of each list to get the next number in the JSON array. Otherwise you will just keep overwriting the first (zero) entry in each list.

Long Answer
Oh so much good stuff in your question! I am a big fan of JSON and Firebase. Here are a few suggestions:

  1. If you are not already familiar with JSON, I just discovered this very helpful article 5. Arrays, Objects, Functions and JSON - Mixu's Node book.
  2. Thunkable LISTs are JSON ARRAYs. In Firebase JSON arrays are stored as a numbered list starting with zero.
  3. You can store lists as either an array or a list of properties. Using a list of properties makes searching faster and data updates easier, but the order of insertion into the list is lost. Arrays preserve the order, but are more difficult to add/update/delete in Thunkable. Here is an example showing first a list as properties (participants) and then a list as an array (Thread).
  4. Thunkable struggles to update existing JSON structures beyond the first level. Some helper functions can make those updates a lot easier. Here are my SET and GET helper functions. They also take care of the asynchronous issues of Save and Get Firebase blocks.
  5. Using The Set Cloud Property function, my example above becomes:
  6. For your purposes, I don’t think the order of images (vs. the name of the image) or the order of the like (vs. who liked the image) matters. The example below does not use arrays, so no numbering is required. Getting the data out is a littler counter-intuitive, as you need to use the Get Object Properties block to find out who “liked” the image.

Happy Thunking!

I’m actually really close.
Just a minimal step is missing.

aiuto_Thunkable

It works perfectly with a list that already exists, but it doesnt create a new list if cloud-appname doesnt exist on RLDB. The IF “cloud-appname is empty” doesnt seem to work within these blocks.

I have also tried to call RLDB Get value, for example “Ibiza”, but it gives me “undefined” even though there’s such a value “Ibiza” in the database.

aiuto_Thunkable

Hope you can help.

You can get the root of the Firebase Tree using //

Use the resulting object to check to see if the property exists:

1 Like

I’ll have another look tomorrow but I have never has any success using if is empty. Use if cloud name = null instead to check if a node exists in rtdb.

1 Like

I think these work in most situations as well for null/exists checking.

image

2 Likes

Thank you very much, that was extremely helpful.
I think i have just completed my app by using your blocks.

1 Like