Sunday, October 16, 2022

[FIXED] why is this Vuex state syntax throwing error?

Issue

I'm quite new with Vuejs & Vuex, I created a local project just to practice, so I have a file called: employeeList with an Array of objects. I'm trying to pass that same Array as state in Vuex, but is throwing me errors. I assume the syntax is wrong, please tell what would be the correct approach and if the problem is in fact the syntax. Thank you & here's the code :

export const employeesModule = {
    namespaced: true,
    state : {
        [
            {
            id: 1, 
            name:'Terry Lawrence',
            username:'TerryLaw',
            email: 'TerryLaw@gmail.com',
            address: 'whateverStreet 258',
            checked: checked.value
            },
            {
            id: 2, 
            name:'MartyClFly',
            username:'MartyMac',
            email: 'MartyMac@gmail.com',
            address: 'George Junior 300',
            checked: checked.value
            },
            {
            id: 3, 
            name:'Nancy Pelosi',
            username:'Drunk ho',
            email: 'drunkHo@gmail.com',
            address: 'Velbedere 400',
            checked: checked.value
            }
]
            
    }, 

The actual file is longer, but there's no purpose to add the mutations, actions, etc...


Solution

Your syntax is wrong on your object "state". If you want it to be an object that contains an array, you need to set it a value like so:

state: {
  newArray: [...]
}

You can't just have an object that contains an array and is not set with a key.



Answered By - Jet.B.Pope
Answer Checked By - Clifford M. (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.