g-viet / til

Today I learned
6 stars 0 forks source link

Firestore: document has undefined type #32

Open g-viet opened 6 years ago

g-viet commented 6 years ago

Firstly, we have users collection and contacts is sub-collection of users. Now, set data for contact with users/userid1/contacts/contactid1 path, and do not set data for user with users/userid1 path.

firestore().collection("users").doc("userid1").collection("contacts").doc("contactid1").set({someAttr: "sample Value"})

Get contact by firestore().collection("users").doc("userid1").collection("contacts").doc("contactid1").get() will return existence data. But get firestore().collection("users").doc("userid1").get() return undefined. If we try to gather documents of users collection by query or get all, undefined document could be ignored.

firestore().collection("users").get().then(usersSnap => {
    usersSnap.docs 
    ...
})

Sometimes, it doesn't need to set data for document, so it should be set as empty. Like this: firestore().collection("users").doc("userid1").set({})