简体   繁体   English

VueJS,Vuex,Getter显示为空数组,但console.log显示它是具有所有值的对象

[英]VueJS, Vuex, Getter is showing as an empty array, but console.log shows it's an object with all the values

This is the method I'm using, pretty simple. 这是我正在使用的方法,非常简单。

DailyCountTest: function (){
  this.$store.dispatch("DailyCountAction")
  let NewPatientTest = this.$store.getters.NewPatientCountGET
  console.log(NewPatientTest)

}

The getter gets that data from a simple action that calls a django backend API. getter通过调用django后端API的简单操作获取数据。

I'm attempting to do some charting with the data so I need to assign them to variables. 我正在尝试对数据进行图表处理,因此需要将它们分配给变量。 The only problem is I can't access the variables. 唯一的问题是我无法访问变量。

This is what the console looks like 这就是控制台的样子

And this is what it looks like expanded. 这就是它的扩展外观。

You can see the contents, but I also see empty brackets. 您可以看到内容,但我也看到了空括号。 Would anyone know how I could access those values? 谁知道我该如何访问这些值? I've tried a bunch of map.(Object) examples and couldn't get any success with them. 我已经尝试了一堆map。(Object)实例,但无法获得任何成功。

Would anyone have any recommendation on how I can manipulate this array to get the contents? 有人会建议我如何操作此数组以获取内容吗?

Thanks! 谢谢!

Here is the Vuex path for the API data 这是API数据的Vuex路径

Action: 行动:

    DailyCountAction ({ commit }) {
      axios({
          method: "get",
          url: "http://127.0.0.1:8000/MonthlyCountByDay/",
          auth: {
            username: "test",
            password: "test"
          }
        }).then(response => {
          commit('DailyCountMutation', response.data)
          })
  },

Mutation: 突变:

    DailyCountMutation(state, DailyCount) {
      const NewPatientMap = new Map(Object.entries(DailyCount));

      NewPatientMap.forEach((value, key) => {
        var NewPatientCycle = value['Current_Cycle_Date']
        state.DailyCount.push(NewPatientCycle)
      });
    }

Getter: 吸气剂:

  NewPatientCountGET : state => {
    return state.DailyCount
  }

State: 州:

    DailyCount: []

This particular description of your problem caught my eye: 对您的问题的这种特殊描述引起了我的注意:

The getter gets that data from a simple action that calls a django backend API getter通过调用Django后端API的简单操作获取数据

That, to me, implies an asynchronous action and you might be getting a race condition. 对我而言,这意味着异步动作,您可能会遇到竞争状况。 Would you be able to post a sample of your getter function to confirm my suspicion? 您是否可以发布您的吸气剂功能样本来证实我的怀疑?

If that getter does indeed rely on an action to populate its contents, perhaps something to the effect of the following might do? 如果该吸气剂确实依赖于一个动作来填充其内容,那么也许以下内容会起作用吗?

DailyCountTest: async () => {
  await this.$store.dispatch('DailyCountAction')
  await this.$store.dispatch('ActionThatPopulatesNewPatientCount')
  let NewPatientTest = this.$store.getters.NewPatientCountGET
  // ... do whatever with resulting array
}

You can also try with a computer property. 您也可以尝试使用计算机属性。 You can import mapGetters 您可以导入mapGetters

import { mapGetters } from 'vuex'

and later in computed properties: 然后在计算属性中:

computed: {
    ...mapGetters(['NewPatientCountGET'])
}

then you can use your NewPatientCountGET and it will update whenever the value changes in the store. 那么您可以使用NewPatientCountGET ,只要存储中的值发生更改,它就会更新。 (for example when the api returns a new value) (例如,当api返回新值时)

Hope that makes sense 希望有道理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM