【问题标题】:How to sove Uncaught (in promise) TypeError: orders.forEach is not a function ? Vue.JS 2如何解决 Uncaught (in promise) TypeError: orders.forEach is not a function ? Vue.JS 2
【发布时间】:2017-08-30 06:49:24
【问题描述】:

我在模块 vuex 存储中的突变是这样的:

const mutations = {
    [types.GET_STATUS] (state,{ orders }) {
        console.log(orders)
        state.status = {}
        orders.forEach(message => {
            set(state.status, message.id, message)
        })
    },
}

console.log(orders) 的结果是这样的:

对象{1:“状态 1”,2:“状态 2”,3:“状态 3”}

执行时出现错误:

Uncaught (in promise) TypeError: orders.forEach 不是函数

我该如何解决?

【问题讨论】:

  • 只是,state.status = orders?

标签: vue.js vuejs2 vue-component vuex


【解决方案1】:

嗯,你不能用forEach 迭代一个对象。 forEach 是一个数组方法。如果您愿意,您可以获取密钥并对其进行迭代。

const keys = Object.keys(orders);
keys.forEach(key => set(state.status, key, orders[key])  

但在这种情况下,不会只是

set(state, status, orders)

工作?我假设set 在这种情况下可用。甚至可能只是

state.status = orders

【讨论】:

  • 非常感谢。只是这样:state.status = orders
猜你喜欢
  • 2017-06-28
  • 2020-10-06
  • 2017-07-05
  • 2017-04-13
  • 2020-10-09
  • 2020-03-17
  • 2020-01-06
  • 2021-03-10
  • 2021-09-30
相关资源
最近更新 更多