Finding a random user from firebase using cloud variable

Hi!

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.

1 Like

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.

1 Like

thanks it appears re-formatted:

{
“userid:AuoJbwU8IDX2VLDbcnfzYbd1hOP2”: {
“DOB”: “2022-11-30”,
“Profilepic”: “http://res.cloudinary.com/dtobv0adm/image/upload/v1669802363/afucxezpx1909lxrik1k.jpg”,
“bio”: “I m jason”,
“name”: “Jason”,
“state”: {
“Feel”: “Sad”,
“Think”: “Holiday”,
“Want”: “Book recommendation”
}
},
“userid:h6Cq8iwu7BSfMHDMttmIxvggSxy2”: {
“DOB”: “2022-11-30”,
“Profilepic”: “http://res.cloudinary.com/dtobv0adm/image/upload/v1669802271/dkuheq26trp7bnzeqpan.jpg”,
“bio”: “Hi I m John”,
“name”: “John”,
“state”: {
“Feel”: “Happy”,
“Think”: “Thinking about weekend”,
“Want”: “Looking for holiday suggestion”
}
}
}

1 Like

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.

For example:
This block

Will get you
image

In standard formats it is expected to show the code without userid:.

1 Like

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?

1 Like

In a journal app I’m making, here is how I structure the user data in Firebase:

users/[user_id]/DOB

Yours is formatted as:

userid:[user_id]/DOB

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?


1 Like

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?

From Medium

3) Only authenticated users can access/write data

// Only authenticated users can access/write data

{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}

The 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 name content.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.