# Finding a random user from firebase using cloud variable

**URL:** <https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051>\
**Category:** Questions about Thunkable X\
**Tags:** firebase\
**Created:** [November 29, 2022, 9:41am UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051 "2022-11-29T09:41:20Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![hesperussingqh](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/hesperussingqh/32/141677_2.png) [@hesperussingqh](https://community.thunkable.com/u/hesperussingqh)\
**Post date:** [November 29, 2022, 9:41am UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/1 "2022-11-29T09:41:20Z")

</div>

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.

 ![Screenshot 2022-11-29 at 5.40.43 PM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/a/a/aa6ff2dba062ce9651b9f4e9ca7e543413927cd8.png)

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [November 29, 2022, 2:03pm UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/2 "2022-11-29T14:03:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [November 29, 2022, 4:15pm UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/3 "2022-11-29T16:15:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hesperussingqh](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/hesperussingqh/32/141677_2.png) [@hesperussingqh](https://community.thunkable.com/u/hesperussingqh)\
**Post date:** [November 30, 2022, 10:06am UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/4 "2022-11-30T10:06:32Z")

</div>

thanks it appears re-formatted:

 ![Screenshot 2022-11-30 at 6.06.09 PM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/9/5/954f3b705d3af7ff3837cffa7d362dab37febacb.png)

{  
“userid:AuoJbwU8IDX2VLDbcnfzYbd1hOP2”: {  
“DOB”: “2022-11-30”,  
“Profilepic”: “[http://res.cloudinary.com/dtobv0adm/image/upload/v1669802363/afucxezpx1909lxrik1k.jpg](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](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”  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [December 1, 2022, 10:45am UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/6 "2022-12-01T10:45:23Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/d/0d7289b1a44aa31d7fc9ee6d91bfacecc57b8680.png)

Will get you  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/d/2d20175053d6f8c40b6348daa051b0c2e7776619.png)

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

---

<div class="post-metadata">

**Author:** ![hesperussingqh](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/hesperussingqh/32/141677_2.png) [@hesperussingqh](https://community.thunkable.com/u/hesperussingqh)\
**Post date:** [December 3, 2022, 2:02pm UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/7 "2022-12-03T14:02:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [December 3, 2022, 3:05pm UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/8 "2022-12-03T15:05:25Z")

</div>

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 &nbsp;|&nbsp; Firebase Realtime Database](https://firebase.google.com/docs/database/web/structure-data#how_data_is_structured_its_a_json_tree)

---

<div class="post-metadata">

**Author:** ![hesperussingqh](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/hesperussingqh/32/141677_2.png) [@hesperussingqh](https://community.thunkable.com/u/hesperussingqh)\
**Post date:** [December 5, 2022, 1:21pm UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/9 "2022-12-05T13:21:09Z")

</div>

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?

 ![Screenshot 2022-12-05 at 9.18.51 PM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/f/f/ff0f2f1460c13fc745b602b7e5aacb9167aa0489.png)  
 ![Screenshot 2022-12-05 at 9.18.42 PM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/a/7/a791bc07d2bcbc11914e769b9bbb6dffe14ddf04.png)

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [December 5, 2022, 8:26pm UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/10 "2022-12-05T20:26:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![hesperussingqh](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/hesperussingqh/32/141677_2.png) [@hesperussingqh](https://community.thunkable.com/u/hesperussingqh)\
**Post date:** [December 6, 2022, 8:57am UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/11 "2022-12-06T08:57:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [December 6, 2022, 2:57pm UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/12 "2022-12-06T14:57:22Z")

</div>

From [https://medium.com/@juliomacr/10-firebase-realtime-database-rule-templates-d4894a118a98:](https://medium.com/@juliomacr/10-firebase-realtime-database-rule-templates-d4894a118a98:)

# 3) Only authenticated users can access/write data

**// Only authenticated users can access/write data**

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

```

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [December 10, 2022, 12:44pm UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/13 "2022-12-10T12:44:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/f/0f59f292712368bce16ff80133ae10de8a6f27e8.png) [@system](https://community.thunkable.com/u/system)\
**Post date:** [March 10, 2023, 12:45pm UTC](https://community.thunkable.com/t/finding-a-random-user-from-firebase-using-cloud-variable/2155051/14 "2023-03-10T12:45:35Z")

</div>

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