如何从firebase获取多个ID?

用户12603373

我有一个管理区域,它从一个数组中收集来自 Firebase 的所有数据。我的任务是更新数据并将其发送到特定用户的 Firebase(我需要发表一些评论)。问题是.doc('id of document'),我不知道如何在 Firebase 中获取文档的特定 ID。如果我输入特定文档的 ID(例如"2jzm4AcWTVNIlT9ESH7V"),函数就可以正常工作doc.data()从 Firebase 返回所有数据,每个 ID 与数据一起存储在对象中。

<script>
import moment from 'moment'
export default{
  // ...
  mounted(){
    db.collection("form").where("posted_at", ">=", 1)
      .get()
      .then(querySnapshot => {
        querySnapshot.forEach(doc=> {
          console.log(doc.id, " => ", doc.data());
          this.array.push(Object.assign({}, doc.data(), {id: doc.id}));
        });
      })
      .catch(function(error) {
        console.log("Error getting documents: ", error);
      });
  },
  methods:{
    comment(){
      let id=this.array.id;
      db.collection("form")
        .doc(id)
        .update({
          comment: this.comment1 //data(){return{comment1:''}}
        })
        .then(function() {
          console.log("Document successfully written!");
        })
        .catch(function(error) {
          console.error("Error writing document: ", error);
        });
    }
  }
};
</script>

PS我收到此功能错误:

FirebaseError: [code=invalid-argument]: Function CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: undefined
编码员

该行let id=this.array.id;试图获取id数组本身属性,而不是数组中条目的 ID。

根据您所关注YouTube 教程,当您comment()v-for循环中附加您的方法时,您应该将与它相关的数组条目作为参数传递。调用此方法时,应设置正在编辑的 ID,加载现有评论(如果存在)并打开模态对话框以编辑评论。

<script>
export default{
  //...
  methods: {
    editComment(userDetails) {
      this.textComment = userDetails.comment || ''; // load current comment
      this.activeUserDetailsId = userDetails.id; // set ID being edited
      $('#commentModal').modal('show'); // show modal dialog
    },
    saveComment() {
      let id = this.activeUserDetailsId;
      if (!id) {
        alert('Failed to save comment - invalid state');
        return;
      }
      db.collection("form")
        .doc(this.activeUserDetailsId)
        .update({
          comment: this.textComment
        })
        .then(function() {
          $('#commentModal').modal('hide');
          console.log("Document successfully written!");
        })
        .catch(function(error) {
          // TODO: Show error to user
          console.error("Error writing document: ", error);
        });
    }
  }
};

我已经编辑,校对和修改了你提供的代码这个更新的文件它是徒手写的,所以如果有任何错误,请告诉我。

变化总结:

  • 重命名变量以明确其用途
  • 添加editComment(userDetails)saveComment()事件处理程序
  • 修复了其他问题formatTime过滤器的使用
  • 添加了对无结果的基本处理
  • 固定缩进
  • 修复了模态的错误放置div- 不应该在v-for循环内

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章