Search code examples
javascriptvue.jsfrontendvuex

why is this Vuex state syntax throwing error?


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: '[email protected]',
            address: 'whateverStreet 258',
            checked: checked.value
            },
            {
            id: 2, 
            name:'MartyClFly',
            username:'MartyMac',
            email: '[email protected]',
            address: 'George Junior 300',
            checked: checked.value
            },
            {
            id: 3, 
            name:'Nancy Pelosi',
            username:'Drunk ho',
            email: '[email protected]',
            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.