如何使用 vuex 操作将数据添加到 firebase 子集合?

How to add data to firebase subcollection with vuex action?

我在实际操作中从 vuex 操作参数访问数据时遇到一些问题。可能有更好的方法来完成我正在尝试做的事情,但目前获取子集合所在文档 ID 的唯一方法是将其传递到 action 参数中。

这样做,我收到一条错误消息:“FirebaseError:函数 CollectionReference.doc() 要求其第一个参数为非空字符串类型,但它是:未定义"

当我在控制台中检查带有 Vue 扩展的组件中的 Vue 数据时,demand.id 在那里:

demandId:"AE5ba0IB1aCwlYDs42ir"

也许我收到这个错误是因为 demandId 是字符串类型?在这一点上,我不确定在哪里寻找答案,也许有人可以通过可能的解决方案指导我?

Vue 组件:

props: ["demand", "modalId"],


data() {
    return {
      meetingForm: {
        demandId: this.demand.id,
        status: "pending",
        date: "",
        time: "",
        duration: null,
        note: ""
      }
    }
  },

//method:

submitModal(closeCallback) {
      const meeting = {
        demandId: this.meetingForm.demandId,
        status: "Pending",
        date: this.meetingForm.date,
        time: this.meetingForm.time,
        duration: this.meetingForm.duration,
        note: this.meetingForm.duration
      };
      // Dispatch action to create Meeting in DB
      this.$store
        .dispatch("meetings/createMeeting", meeting)
        .then(_ => {
          closeCallback();
          this.$toasted.success("Meeting has been succesfully created!", {
            duration: 3000
          });
        })
        .catch(e => console.error(e));
    }

Vuex 动作:

createMeeting( meeting) {
      meeting.createdate = Timestamp.fromDate(new Date())
      const demandId = meeting.demandId
      console.log(demandId)
      return db
        .collection('demands')
        .doc(this.demandId)
        .collection('meetings')
        .add(meeting)
      },

用 demandId

替换 this.demandId
createMeeting( meeting) {
      meeting.createdate = Timestamp.fromDate(new Date())
      const demandId = meeting.demandId
      console.log(demandId)
      return db
        .collection('demands')
        .doc(demandId)
        .collection('meetings')
        .add(meeting)
      },

Vuex API

动作 类型:{ [类型:字符串]:函数}

在商店注册操作。处理函数接收一个公开以下属性的上下文对象:

{ state, // 与 store.state 相同,如果在模块中则为本地状态 rootState, // 与 store.state 相同,仅在模块中 commit, // 与 store.commit 相同 dispatch, // 与 store.dispatch 相同 getters, // 与 store.getters 相同,如果在模块中则为本地 getters rootGetters // 与 store.getters 相同,仅在模块中 } 如果有的话,还会收到第二个有效负载参数。

问题是我没有指定上下文。