I am trying to create a block where I would find a random user from the firebase and show their details (e.g. name, bio etc).
Trying to understand about the json and cloud variable but I am confused.
How should I go about that?
Here is my json.
Data returned by Firebase are already in Thunkable object format. In principle, you can loop through the data and extract the information required but your two sets of data are not identical in format. This will cause an error resulting on the object in Thunkable becoming Null.
I think they probably are the same and that the screenshot includes a typo (a bracket in the wrong place).
@hesperussingqh Can you paste your JSON text into https://codebeautify.org/jsonviewer and make sure it appears re-formatted on the right side of the screen? If that works as valid JSON, paste it into a post here and we can help you further.
thanks it appears re-formatted:
“bio”: “I m jason”,
“Want”: “Book recommendation”
“bio”: “Hi I m John”,
“Think”: “Thinking about weekend”,
“Want”: “Looking for holiday suggestion”
The format of the JSON is still abnormal, the userId with the code of the the user is separated by a colon but in the same time it is used as a key only not a key:value pair. It still can be used but not in a direct way.
Will get you
In standard formats it is expected to show the code without
I added the userid: cos i wasn’t sure how to distinguish it’s a user database in firebase lol
but sounds like i am doing sth unnecessary
stupid question. what is the app variable nameRTDB pointing to?
and should i be using cloud variable here?
In a journal app I’m making, here is how I structure the user data in Firebase:
Yours is formatted as:
Creating the “users” key at the top level and having all user ids below it allows you to access the user ids as a list which is helpful for searching, etc.
You can see an example of that in the Firebase best practices documentation:
Structure Your Database | Firebase Realtime Database
thanks @tatiang super helpful tips.
Now I redo the data structure…I tried to get the random user name like the below blocks but it doesn’t work.
Can you help to see what’s wrong?
I would think those blocks would work. Hmm… Does it make any difference if you remove the initialize block and instead use cloud variable “users” in place of the
cloud variable users block? I would think not but that’s the only difference with my code. Do you have access to users/ in Thunkable? Because once you add sign in and authentication for Firebase, you often have to change the Firebase rules to allow access only to users/userid and not users.
For example, can you set the label’s text to get property “name” of object cloud variable “users/nbNp9KpkjaOVOQBDCZE2akJBfW02/name”? Does that work?
You are right…I can’t set the label text according to what you said.
But my firebase rules are
“.read”: “auth != null”,
“.write”: “auth != null”
what do i need to change to?
3) Only authenticated users can access/write data
// Only authenticated users can access/write data
".read": "auth != null",
".write": "auth != null"
get object properties block actually strips off all underlying data and therefore the resulting data set will only be the 2 user codes without anything under them. This will lead to user block failing to get the