简体   繁体   中英

JSON/Firebase Data Structure

I've got the rough idea of how to create and retrieve data using Firebase. However, what I'm not 100% certain of is the best way to reference different users. On their site, there is an example of saving user data which leads to a json tree like this:

{ "users": { "alanisawesome": { "date_of_birth": "June 23, 1912", "full_name": "Alan Turing" }, "gracehop": { "date_of_birth": "December 9, 1906", "full_name": "Grace Hopper" } } }

What confuses me are the "alanisawesome" and "gracehop" references. Initially I thought I should be replacing these with the unique IDs created when making the user, as in:

"users": { "12349WEF": { "date_of_birth": "June 23, 1912", "full_name": "Alan Turing" }

But then I came across another JSON tree example, which had the UID under the depth as other personal info as their name, email, etc. (It wasn't clear as to what the parent of the personal data was). Also from Firebase's docs is a method childByAutoId that generates a timestamped ID. If this is not what I use to make users, what would be the proper setup for me to reference a given user and access his or her info?

What confuses me are the "alanisawesome" and "gracehop" references. Initially I thought I should be replacing these with the unique IDs created when making the user

The most common way to store user information in a Firebase Database is to have a structure like:

/users
    $uid
       full_name: "Alan Turing"
       date_of_birth: "June 23, 1912"

Where I use $uid in the above snippet, you'd use the uid that Firebase Authentication generates for you. This is a long, opaque string. For example "e836f712-4914-41df-aa80-5ade6b8b7874" . Since this is not very readable, the Firebase documentation tends to use "fake" uids, such as "alanisawesome" and "gracehop" in the example you quoted. For more information see the storing user data section in the Firebase documentation . Keep in mind that all of this is just a (frequently used) convention. You'll need to figure out if it works for you use-cases and adapt it where needed.

Also from Firebase's docs is a method childByAutoId that generates a timestamped ID. If this is not what I use to make users, what would be the proper setup for me to reference a given user and access his or her info?

If items have a natural key, it is common to store them under that key. Users are identified by their user id (uid in the Firebase API) and thus are typically stored under that id in the database. If you need to also look up users by one of their other properties, you'd create a so-called index that maps the other property to the natural/primary key. Ie

/usernames
    "Alan Turing": "alanisawesome"
    "Grace Hopper": "gracehop"

You'll note that once again I use the "fake" uids in this example for enhanced readability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM