A
A
Andrey2017-08-07 17:29:35
JavaScript
Andrey, 2017-08-07 17:29:35

VueJS Mutations and Actions - payload object not being passed, why?

There are three users.

users: [
      {id: 1, name: "Andrey", registered: false},
      {id: 2, name: "Ivan", registered: false},
      {id: 3, name: "Max", registered: false}
]

There is a method in the component
methods: {
    register(user) {
        console.log('in method user id = ', user.id)
        this.$store.dispatch('register', {
            payload: {
                type: 'register',
                user
            }
        })
    }
}

Accordingly, there is an Action:
register({commit}, payload) {
    commit('register', payload)
}

And a mutation to it:
register(state, payload) {
    const user = state.users.find(user => {
      return user.id === payload.user.id
    })
    user.registered = true;
    const registration = {
      userId: payload.user.id,
      name: user.name
    }
    state.registrations.push(registration)
  }

What is the problem:
In mutation already on the first line
const user = state.users.find(user => {
    return user.id === payload.user.id
})

gives an error that payload.user is of type undefined (Cannot read property 'id' of undefined). Accordingly, it is still even in Action'e undefined type. Why is this happening? Why can I directly pass the user object , but I can't pass the user object in the payload object ?
I'm doing something wrong, I'm waiting for your comments ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2017-08-07
@andrey_koyta

The question is closed, I blunted it myself ...
The error was in this code:

this.$store.dispatch('register', {
     payload: {
            type: 'register',
            user
     }
})

To be more precise, for some reason I wrote in the OBJECT payload: {another object}
Correctly like this:
this.$store.dispatch('register', {
            type: 'register',
            user
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question