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.
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:
This is the latest i have tried so far, but i have tried many without success.
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
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.
Add your Firebase credentials in settings and have a look directly at the list being created in the database.
you are right, my fault i attached the wrong picture.
That was i was doing
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.
Oh so much good stuff in your question! I am a big fan of JSON and Firebase. Here are a few suggestions:
- 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.
- Thunkable LISTs are JSON ARRAYs. In Firebase JSON arrays are stored as a numbered list starting with zero.
- 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).
- 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.
- Using The Set Cloud Property function, my example above becomes:
- 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.
I’m actually really close.
Just a minimal step is missing.
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.
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:
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.
I think these work in most situations as well for null/exists checking.
Thank you very much, that was extremely helpful.
I think i have just completed my app by using your blocks.