Firestore函数或触发器之间的设计决策



我使用的客户端平台是WebAngular。

当我创建一个任务时,我会在其中添加追随者,例如3个追随者。

现在,一旦保存了Task,我想在3个不同的文档中推送3条记录。

  1. 是否在客户端创建for循环
  2. 使用Firestore触发器?它处于测试阶段,可能需要长达10秒的延迟
  3. Firestore函数

满足此要求的最佳方法是什么?

编辑1

如何在批量提交中安排array union代码?

我当前的代码

var washingtonRef = firebase.firestore().collection("notifications").doc(this.loggedInuser);
washingtonRef.update({
notifyArray: firebase.firestore.FieldValue.arrayUnion(
{ food: "Margarita", ctg: "Pizza" },
{ food: "Chicken Burger", ctg: "Burger" },
{ food: "Veg Burger", ctg: "Burger" }
)
});

批量

batch_write(){
// Get a new update batch
var batch = firebase.firestore().batch();
var sfRef = firebase.firestore().collection("notifications").doc("1");
batch.update(sfRef, **HOW DO I PLACE here arrayUnion like above** ??);
//another update batch
// and another update batch
// Commit the batch
batch.commit().then(function () {
console.log("batch commited successful");
});
}

如果我像下面这样做,它会给出错误Cannot find name 'notifyArray'.-

var sfRef = firebase.firestore().collection("notifications").doc("1");
batch.update(sfRef, 
notifyArray: firebase.firestore.FieldValue.arrayUnion(
{ food: "Margarita", ctg: "Pizza" },
{ food: "Chicken Burger", ctg: "Burger" },
{ food: "Veg Burger", ctg: "Burger" }
)  
);

最简单的方法是使用批量写入来写入/更新四个文档(写入Task 1和写入/更新三个follower文档(。

您必须执行以下操作:

var batch = firebase.firestore().batch();
var sfRef = firebase.firestore().collection("notifications").doc("1");
batch.update(sfRef, 
{ notifyArray: firebase.firestore.FieldValue.arrayUnion(
{ food: "Margarita", ctg: "Pizza" },
{ food: "Chicken Burger", ctg: "Burger" },
{ food: "Veg Burger", ctg: "Burger" }
)
}  
);
var sfRef = firebase.firestore().collection("notifications").doc("2");
batch.update(sfRef, 
{ notifyArray: firebase.firestore.FieldValue.arrayUnion(
{ food: "Margarita", ctg: "Pizza" },
{ food: "Chicken Burger", ctg: "Burger" },
{ food: "Veg Burger", ctg: "Burger" }
)
}  
);
batch.commit().then(function () {
console.log("batch commited successful");
});

最新更新