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.
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.
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.
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.
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.