Javascript Firestore arrayunion是否提供任何回调?

Javascript Firestore arrayunion是否提供任何回调?,javascript,firebase,vue.js,google-cloud-firestore,nuxtjs,Javascript,Firebase,Vue.js,Google Cloud Firestore,Nuxtjs,嗨,我正在建立某种投票系统,我想阻止同一个用户在同一个帖子上投票 let db = firebase.firestore(); var postRef = db.collection("posts").doc(this.pid); postRef.update({ votes: firebase.firestore.FieldValue.increment(1) }); var userRef = db.collection("users

嗨,我正在建立某种投票系统,我想阻止同一个用户在同一个帖子上投票

  let db = firebase.firestore();
  var postRef = db.collection("posts").doc(this.pid);
  postRef.update({
    votes: firebase.firestore.FieldValue.increment(1)
  });
  var userRef = db.collection("users").doc(this.userId);
  userRef.update({
    votes: firebase.firestore.FieldValue.arrayUnion(this.pid)
  });
  //run this line if pid is added
  this.votes = this.votes + 1;
我只想在投票数组中添加pid时增加投票。我想知道arrayUnion是否能够就此提供某种反馈,或者我是否可以这样做


你可以看到,同一个人可以在同一个帖子上多次投票。

不幸的是,通过设计
increment
arrayUnion
不提供任何回调

  let db = firebase.firestore();
  var postRef = db.collection("posts").doc(this.pid);
  postRef.update({
    votes: firebase.firestore.FieldValue.increment(1)
  });
  var userRef = db.collection("users").doc(this.userId);
  userRef.update({
    votes: firebase.firestore.FieldValue.arrayUnion(this.pid)
  });
  //run this line if pid is added
  this.votes = this.votes + 1;
为了实现您的需求,您需要一个事务(由引擎盖下的
increment
arrayUnion
使用):

const postRef=db.collection(“posts”).doc(this.pid);
const userRef=db.collection(“users”).doc(this.userId);
db.runTransaction(异步(t)=>{
const post=等待t.get(postRef);
const user=wait t t.get(userRef);
如果(!user.get('voates')。包括(this.pid)){
t、 更新(postRef,{voces:post.get('voces')+1});
t、 更新(userRef,{投票:[…user.get('voates'),this.pid]});
}
});