Asked By: Anonymous
I have a collection named “doctors” with fields like name, id, username…
Inside doctors there is a field “medicalcenter” that has a reference to the collection named “medicalcenters” with fields like address and name of the medical center…
What I want is. When I get a list of doctors I want also to get the medical center name with this document reference that I have inside the doctors collection.
doc.data().medicalcenter inside doctors collections has the reference.
getdoctors () {
firebase.firestore().collection('doctor').orderBy('id').get().then(result => {
result.forEach(doc => {
const data = {
'id': doc.data().id,
'nome': doc.data().nome,
'email': doc.data().email,
'username': doc.data().username
'medicalcenter': HERE I WANT THE MEDICAL CENTER NAME
}
this.items.push(data)
})
})
}
How is it possible to make this happens?
How can I get the specific collcetion and the field name using the collection reference stored in the doctors?
Do I have to user 2 methods Async?
Solution
Answered By: Anonymous
This is how I managed to create this function.
The getDoctor async function will wait the await medicalcenter.
This way I can get the medical center that each doctor works.
created () {
this.getdoctors()
},
methods: {
getdoctors: async function () {
try {
let result = await firebase.firestore().collection('doctor').get()
result.forEach(async doc => {
console.log(doc.data())
var medicalcenter = await firebase.firestore().collection('medicalcenter').doc(doc.data().centrosaude.id).get()
const data = {
'id': doc.data().id,
'nome': doc.data().nome,
'email': doc.data().email,
'centrosaude': medicalcenter.data().nome
}
this.items.push(data)
})
} catch (error) {
console.log(error.message)
}
}
}