How to sove Uncaught (in promise) TypeError: orders.forEach is not a function ? Vue.JS 2

How to sove Uncaught (in promise) TypeError: orders.forEach is not a function ? Vue.JS 2

我在 vuex store 模块中的修改是这样的:

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

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

Object {1: "status 1", 2: "status 2", 3: "status 3"}

执行时出现错误:

Uncaught (in promise) TypeError: orders.forEach is not a function

我该如何解决?

嗯,你不能用 forEach 遍历一个对象。 forEach 是一个数组方法。如果你愿意,你可以抓住键并迭代它们。

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

但在这种情况下,不只是

set(state, status, orders)

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

state.status = orders