Reputation: 3128
I'm trying to study Android Studio from a textbook. There was the following code for SIGN-UP:
Map<String, Object> user = new HashMap<>();
user.put("email", mAuth.getCurrentUser().getEmail());
db.collection("users")
.document(mAuth.getCurrentUser().getUid())
.set(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void v) {
//code
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//code
}
});
I'm trying to figure out how they wanted the DB to look like. I'm using firebase cloud firestore. As I understand I need to create a collection called users
and that what I did. Then I got to the following window:
What should I do next? As I understand from the code, I get the document by using user's UID but I don't have it so what should I fill in the fields? How the JSON should look like?
Upvotes: 0
Views: 85
Reputation: 394
The Firebase Firestore is a new data model, more sophisticated than the Realtime Database (JSON tree). In a nutshell, the Firestore is a bunch of data organized in collections, documents and subcollections.
The root path must have only collections (you create one called users
). The collections must have documents, think them like objects or maps, with key-value pairs.
A document cannot have more than 1 megabyte of data itself, however, a document can have subcollections and this is the way that you have to access this new data model called Firebase Firestore, always alternating between collection and document.
I recommend the Youtube Firestore playlist to understand more clearly this new database.
PS: It no generate a JSON tree-like Realtime Database.
Upvotes: 0
Reputation: 13129
When you do .set()
on your collection, it will create a document with your generated User id
db.collection("users")
.document(mAuth.getCurrentUser().getUid())
.set(user)
Here you are telling that inside the collection users, create a document which id is the current logged in userID and then set the user object as the data of that document
After you run the code you posted, it will generate by itself the user in that collection, if you don't see it directly at your Firebase console, refresh your browser
What should I do next? As I understand from the code, I get the document by using user's UID but I don't have it so what should I fill in the fields?
This is wrong, you are not getting any user because you are using .set()
if you want to get that user document you will need to change that to .get()
Check the official docs if you have any doubt https://firebase.google.com/docs/firestore/quickstart?hl=en
Upvotes: 1