首页 > 解决方案 > 从 Firebase 实时更新

问题描述

我的目标是从 firebase 实时更新。我需要从 firebase 获得评论并在 this.note 上更新它。在函数 update() 的某个地方我做错了。我遵循的教程。

methods: {
update(){
db.collection("form").onSnapshot((querySnapshot)=>{
    this.userdata1=[]
    querySnapshot.forEach((doc)=>{
      this.userdata1.push(doc.data().comment);
    });
  });
},
editComment(userDetails) {
  $('#commentModal').modal('show'); 
  this.note = userDetails.comment || ''; 
  this.activeuser = userDetails.id; 
},
saveComment() {
  let id = this.activeuser;
  if (!id) {
    alert('Fail');
    return;
  }
  db.collection("form")
    .doc(this.activeuser)
    .update({
      comment: this.note
    })
    .then(() =>{
      this.update();
      $('#commentModal').modal('hide');
      console.log("Document successfully written!");
    })
    .catch(function(error) {
      console.error("Error writing document: ", error);
    });
}
}

完整代码:https ://pastebin.com/tx5F0zz8

标签: javascriptnode.jsfirebasevue.jsgoogle-cloud-firestore

解决方案


您错过了 4:38 视频中的 watcher() 函数。


推荐阅读