This works:
db.collection(\'users\').doc(\'id\').get()
.then((docSnapshot) => {
if (docSnapshot.exists) {
db.collection(\'users\').doc(\'id\')
Your initial approach is right, but it may be less verbose to assign the document reference to a variable like so:
const usersRef = db.collection('users').doc('id')
usersRef.get()
.then((docSnapshot) => {
if (docSnapshot.exists) {
usersRef.onSnapshot((doc) => {
// do stuff with the data
});
} else {
usersRef.set({...}) // create the document
}
});
Reference: Get a document