I have 2 collections in my firestore. One called owners and the second is unicorns. An owner has only a name field and a unicorn has a name and a reference to the owner. I want a query to return an array of objects that looks like this
unicorns = { id: 123,
name: Dreamy,
owner: { id: 1
name: John Cane
}
}
my query looks like this but there is something missing that I can't figure out
let unis = [];
db
.collection("unicorns")
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
let uni = {
id: doc.id,
name: doc.data().name,
owner: doc.data().owner,
};
if (uni.owner) {
doc
.data()
.owner.get()
.then((res) => {
uni.owner = {
id: res.id,
name: res.data().name,
};
unis.push(uni);
})
.then(() => {
setUnicorns(unis);
})
.catch((err) => console.error(err));
} else {
unis.push(uni);
}
});
})
When I setUnicorns hook and I try to map the results I don't get all the data I need