I am querying some documents from firestore. how do I get user info from the snapshot object.
The information like the user ID and user name.
Assuming that the user signed in using a social OAuth provider . IF it matters.
firebase.firestore().collection('sample').get()
.then(function(snapshot) {
console.log('SNAPSHOT', snapshot);
snapshot.forEach(function(doc) {
console.log(doc.exists);
console.log(doc);
console.log(doc.id);
console.log(doc.metadata);
console.log(doc.ref);
console.log(doc.data());
console.log(doc.ref.path);
console.log(doc);
})
}).catch(console.log);
I am querying some documents from firestore. how do I get user info from the snapshot object.
The information like the user ID and user name.
Assuming that the user signed in using a social OAuth provider . IF it matters.
firebase.firestore().collection('sample').get()
.then(function(snapshot) {
console.log('SNAPSHOT', snapshot);
snapshot.forEach(function(doc) {
console.log(doc.exists);
console.log(doc);
console.log(doc.id);
console.log(doc.metadata);
console.log(doc.ref);
console.log(doc.data());
console.log(doc.ref.path);
console.log(doc);
})
}).catch(console.log);
Share
Improve this question
edited Feb 4, 2019 at 15:17
Frank van Puffelen
600k85 gold badges889 silver badges859 bronze badges
asked Feb 4, 2019 at 14:14
UVicUVic
1,8113 gold badges16 silver badges22 bronze badges
3
- Firebase does not automatically create documents for signed in users. So if such a document exists, it's because your application created them. If you indeed did create the document elsewhere, edit your question to include either the code that creates the user documents, or a screenshot of a relevant document in the Firestore console. – Frank van Puffelen Commented Feb 4, 2019 at 15:19
- 1 The question was on any document like articles , post , ments etc.. how to get the user who created them. Should I explicitly add the user ID when I create any document for other collections. – UVic Commented Feb 4, 2019 at 15:48
- 1 Firestore does not associate documents with users. If you want to associate the documents with a user, you can either add a field in each document that holds the user, use the UID of the user in/as the document name, or store the documents for a user in a subcollection. – Frank van Puffelen Commented Feb 4, 2019 at 15:56
3 Answers
Reset to default 5Firestore does not associate documents with users. If you want to associate the documents with a user, you will have to do this in your application code. You can either add a field in each document that holds the user, use the UID of the user in/as the document ID, or store the documents for a user in a subcollection.
If you add a field in each document that holds the user, you can then get all documents associated with that user with:
firebase.firestore().collection('sample').where('uid', '=', firebase.auth().currentUser.uid).get()...
If you use the UID of the user as the document ID, you can get a user's document with:
firebase.firestore().collection('sample').doc(firebase.auth().currentUser.uid)...
If you store the documents for a user in a subcollection of a document named after the user, you can get at that collection with:
firebase.firestore().collection('sample').doc(firebase.auth().currentUser.uid).collection('documents')...
As Frank van Puffelen pointed out your application needs to implement the logic to create user-documents on Firestore/Firebase RTDB.
The work you have to do is: 1.) create a user account in firebase-backend under 'authentication'. You can give any email bined with any password - those are not real google accounts and just used for your app.
2.)Write the Login-Logic, so that you can log in to your app with your previously created account. Look for an onSuccess message from the server to be sure that everything works.
3.) If a user is logged in, you can retrieve an firebaseAuth.currentUser object from your app and get additional metaData, such as a timestamp of last login. Also, you can now retrieve his email and possibly create a unique ID. With this unique key you can now create new user-records with your own individual data. a typical structure would be:
-your app namespace
--users
[email protected]
----displayName: frank
----age: 47
----loves to eat: spaghetto-code
[email protected]
...
--data
...
Hope this helps!
You need to write backend code using Firebase admin SDK. See https://firebase.google./docs/auth/admin/manage-users
admin.auth().getUser(uid)
.then(function(userRecord) {
// See the UserRecord reference doc for the contents of userRecord.
console.log('Successfully fetched user data:', userRecord.toJSON());
})
.catch(function(error) {
console.log('Error fetching user data:', error);
});