【问题标题】:How to save JUST a value from a firestore document in Vuex instead of the whole object?如何仅从 Vuex 中的 firestore 文档中保存一个值而不是整个对象?
【发布时间】:2020-03-17 22:46:37
【问题描述】:

我有一个从 firestore 文档中检索数据的 Vue 组件,我尝试将文档的值保存为 vuex 状态,但不是只保存文档的值,而是保存整个对象,我的意思是字段名和值。

这是我的组件代码:

created() {
    var docRef = db.doc("rates/ratesData");
    docRef
      .get()
      .then(doc => {
        if (doc.exists) {
          this.$store.commit("setRate", doc.data());
        } else {
          console.log("No such document!");
        }
      })
      .catch(function(error) {
        console.log("Error getting document:", error);
      });
  }

Store.js:

export default new Vuex.Store({
  state: {
    currentRate: null
  },
  mutations: {
    setRate(state, value) {
      state.currentRate = value;
    }
  }
});

我希望状态如下所示: I can't post images yet

但它看起来像这样: I still can't post images yet

我的英语不好,对不起。

【问题讨论】:

  • 您能否编辑问题以明确您希望它做什么而不是现在做什么? doc.data() 返回一个包含文档中所有字段的对象。你想要一些不同的东西吗?
  • 是的,而不是整个对象,我只想要对象的值...对象返回rateStatus: 75662.78我只希望它返回75662.78

标签: vuejs2 google-cloud-firestore vue-component vuex


【解决方案1】:

不用传递doc.data(),只需传递你想要的文档对象的属性。试试doc.data().rateStatus

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-22
    • 2019-09-03
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    相关资源
    最近更新 更多