I'm building a forum appin Vue, basically a clone of this same site... The user can post questions, get answers to those questions and comments to those answers. I have a top level collection for questions, answers and comments respectively, and each document in those collections have a field called photoURL
where I store the url of the profile image of the author of that question/answer/comment.
Now, I have this code to update the photoURL
field in each document from the same author in the three collections when a user update their profile image:
const QUESTIONS = db.collection("questions");
QUESTIONS.where("authorId", "==", this.user.data.id)
.get()
.then(snapshots => {
if (snapshots.size > 0) {
snapshots.forEach(questionItem => {
QUESTIONS.doc(questionItem.id).update({
photoURL: imgUrl
});
});
}
});
const ANSWERS = db.collection("answers");
ANSWERS.where("authorId", "==", this.user.data.id)
.get()
.then(snapshots => {
if (snapshots.size > 0) {
snapshots.forEach(answerItem => {
ANSWERS.doc(answerItem.id).update({
photoURL: imgUrl
});
});
}
});
const COMMENTS = db.collection("comments");
COMMENTS.where("authorId", "==", this.user.data.id)
.get()
.then(snapshots => {
if (snapshots.size > 0) {
snapshots.forEach(commentItem => {
COMMENTS.doc(commentItem.id).update({
photoURL: imgUrl
});
});
}
});
It currently works, but there is a way to make these three operations at once? Or a better way in general to achieve those updates? I was thinking in storing all the ids retrieved from the three collections in an array first and then make the updates, but the numbers of writes would remain the same, so it doesn't looks like an improve.