Design decision between Firestore functions or triggers [closed]

别说谁变了你拦得住时间么 提交于 2020-04-30 06:25:27

问题


Client side platform I am using is Web Angular.

Brief explanation: when I create a Task, I add followers in it, for example 3 followers.

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

  1. Do I create a for loop at client side?
  2. Use Firestore triggers? It's in beta and can take up to 10 seconds delay
  3. Firestore functions ?

Please share your best experience to cope up with this requirement.

Edit 1

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

My current code

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

In batch

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

}

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

回答1:


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).

You have to do as follows:

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


来源:https://stackoverflow.com/questions/61487697/design-decision-between-firestore-functions-or-triggers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!