如何从Firebase获取多个ID? [英] How to get multiple id's from firebase?

查看:75
本文介绍了如何从Firebase获取多个ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an admin area which collects all data from Firebase in an array. My task is to update data and send it to Firebase for specific users (I need to put some comment). Problem is that in .doc('id of document'), I don't know how to get the specific ID of document in Firebase. Function works fine if I put in an ID of a specific document (e.g. "2jzm4AcWTVNIlT9ESH7V"). doc.data() returns all the data from Firebase and each ID is stored in the object together with the data.

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

P.S.我收到此错误的函数:

P.S. i get this error for function:

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.

The line let id=this.array.id; is trying to get the id property of the array itself, not the ID of an entry in the array.

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

Based on the YouTube tutorial you are following, when you attach your comment() method in the v-for loop, you should pass the array entry it relates to in as a parameter. When this method is invoked, it should set the ID that is being edited, load the existing comment (if it exists) and open the modal dialog to edit the comment.

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

我已经将您提供的代码编辑,校对并重新制作为

I have edited, proofed and reworked the code you provided into this updated file. It was written free hand so let me know if there are any bugs.

更改摘要:

  • Renamed variables to make their use clear
  • Added editComment(userDetails) and saveComment() event handlers
  • Fixed usage of formatTime filter from your other question
  • Added basic handling of no results
  • Fixed indentation
  • Fixed incorrect placement of your modal div - shouldn't have been inside v-for loop

这篇关于如何从Firebase获取多个ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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