Firestore功能或触发器之间的设计决策 [英] Design decision between Firestore functions or triggers

查看:53
本文介绍了Firestore功能或触发器之间的设计决策的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的客户端平台是Web Angular.

Client side platform I am using is Web Angular.

创建任务时,我在其中添加了关注者,例如3个关注者.

When I create a Task, I add followers in it, for example 3 followers.

现在,保存任务后,我想在3个不同的文档中推送3条记录.

Now once Task is saved, I want to push 3 records in 3 different documents.

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

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

What is the best way to cope with this requirement?

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

How do I arrange the array union code in batch commit ?

我当前的代码

  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");
});

}

如果我按照以下方式进行操作,则会出现错误找不到名称'notifyArray'.-

If i am doing it like below, it gives error 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" }
    )  
  );

推荐答案

最简单的方法是使用

The easiest way would be to use a batched write to write/update the four documents (writing the Task one and writing/updating the three followers documents).

您必须执行以下操作:

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");
});

这篇关于Firestore功能或触发器之间的设计决策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆