Answer the question
In order to leave comments, you need to log in
Firestore: how to get the document ID when listening to a collection?
Hello!
Not quite sure how to solve the following problem, namely how to get the auto-generated document ID when listening to a collection? That is, we have the following code:
created() {
// Get current user and get info from db
let currentUser = firebase.auth().currentUser
this.$store.dispatch('setUserId', currentUser.uid)
let ref = db.collection('categories').where('user_id', '==', currentUser.uid).orderBy('category_name')
ref.onSnapshot(snapshot => {
// snapshot.forEach(doc => {
// тут у нас есть doc.id
// return this.$store.commit('addCategory', {category_name: doc.data().category_name, user_id: doc.data().user_id})
// })
snapshot.docChanges().forEach(change => {
if (change.type == 'added') {
// тут какой-либо doc.id или change.id у нас нет
let doc = change.doc
return this.$store.commit('addCategory', {category_name: doc.data().category_name, user_id: doc.data().user_id})
}
})
})
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question