[Vuejs]-Array is not array anymore when passed into Vuex action function

0πŸ‘

βœ…

I finally understood what my issue was after taking another look at the object that was being logged on the console. I did not know that Vuex actions HAD to have two arguments if you want to pass in a payload into that function. For example, I initially did this

filterEvents(events) {
    .....
}

but what I really needed to do was

filterEvents(context, events) {
    .....
}

The context argument is the object that allows you to do things such as commit and dispatch. I usually destructure the context object (i.e. { commit, dispatch} ), so I for some reason never thought twice about it. You don’t have to destructure the context object to use commit and dispatch; if you don’t it would just be like

context.commit('function', payload);

0πŸ‘

I always try to check the length prop of the array which helps me out in such cases.

...
return new Promise((resolve, reject) => {
        if (!Array.isArray(events) && !events.length) {
            reject("Invalid argument: is not of type Array.");
        }

        .....
    });
...

Leave a comment