Props value can't be passed as argument to store action

Hi all, this is my first post on this forum! :slight_smile:
This is the problem I have:

I have customer details page (vue component - CustomerDetails.vue) on which I get customer id by using props:true on my router.
So when I have e.g. http://mypage/customers/3, I get it like this on CustomerDetails page:

props: ['id']

and I can print it in console with

console.log(this.id) // prints 3

Now I want to delete this customer by pressing delete button on the page (which shows modal with yes/no), and then redirect to all customers page (/customers), if yes was pressed.

On “are you sure modal” I emit “delete-customer” to CustomersDetails.vue component and then trigger the deleteCustomer method like this:

methods: {
...
deleteCustomer() {
this.$store.dispatch('customers/deleteCustomer', this.id); //customers is a namespace
}

but on actions.js:

deleteCustomer(payload) {
console.log(payload.id)

I get undefined in console. :frowning:

Not sure what I am doing wrong.

You’re passing the ID directly as your payload, rather than passing an object with the ID in it. You’re currently logging the value as if you’ve passed: { id: id } to your payload.

deleteCustomer (payload) {
   console.log(payload) //  should return the id
}

If you’re not passing an object, then name your params to what you’re expecting:

deleteCustomer (id) {
   console.log(id)
}
1 Like

Hi w3jay, thx for reply. I did try all of the combination including yours but the problem was not resolved.
Yesterday I figured our that I should pass 2 arguments to deleteCustomer like:

deleteCustomer (context, payload) {
console.log(payload)
}

or to omit it like this:

deleteCustomer (_, payload) {
console.log(payload)
}

and now I get my id properly. Looks like I was trying to print context object.

Also I have changed passing this.id to {id: this.id} as you suggested and now accessing it as payload.id.

Thx!

1 Like

Yes, this won’t work, from 1st post, as I am trying to access property on non object. :slight_smile:

1 Like