【发布时间】:2021-04-27 21:53:59
【问题描述】:
我是 Firestore 的新手,想知道是否有人可以告诉我此解决方案对于多对多关系是否可行。我有一组花名册和一组与多对多相关的学生。由于我最常需要的关于学生的信息只是他们的姓名,将像 {
我的解决方案基于this answer。
如果有任何建议,我将不胜感激!谢谢
【问题讨论】:
我是 Firestore 的新手,想知道是否有人可以告诉我此解决方案对于多对多关系是否可行。我有一组花名册和一组与多对多相关的学生。由于我最常需要的关于学生的信息只是他们的姓名,将像 {
我的解决方案基于this answer。
如果有任何建议,我将不胜感激!谢谢
【问题讨论】:
对此进行更新,它工作正常。如果将来有人需要,这是我的云功能代码,用于更新运动员姓名:
export const onUserUpdate =
functions.firestore.document("users/{user}/athletes/{athlete}").onUpdate(
async (change) => {
const after = change.after.data();
const before = change.before.data();
const bid = change.before.id;
console.log("BID: ");
console.log(bid);
const userId: any = change.before.ref.parent.parent?.id;
console.log(`users/${userId}/rosters`);
if (after.athleteName != before.athleteName) {
console.log("Change name detected");
const snapshot =
await db.collection(
`users/${userId}/rosters`).where(
`athletes.${bid}`, ">=", "").get();
const updatePromises : Array<Promise<any>> = [];
snapshot.forEach((doc) => {
console.log(doc.id);
updatePromises.push(db.collection(`users/${userId}/rosters`)
.doc(doc.id).update(`athletes.${bid}`, after.athleteName)
);
});
await Promise.all(updatePromises);
}
});
【讨论】: