N
N
Nikolay Semenov2017-04-03 16:20:49
Vue.js
Nikolay Semenov, 2017-04-03 16:20:49

Why is the object not being removed from the array?

Hi all!
there is a mutation

[types.REMOVE_FROM_CART] (state, { id }) {
        state.lastCheckout = null
        const record = state.added.find(p => p.id === id)
        if (typeof record == 'object')
            if (record.quantity == 1) {
                state.added.remove(record)
            } else {
                record.quantity--;
            }

when the number is greater than 1, then this branch works fine, i.e. decreases by one, but when it is equal to one and you press delete, it throws an error:
Uncaught TypeError: state.added.remove is not a function
Am I deleting somehow wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wostex, 2017-04-03
@nickola105

state.added = array? then you can do this:

if (typeof record == 'object') {
    if (record.quantity == 1) {
        let i = state.added.findIndex(p => p.id === id);
        state.added.splice(i,1);
        //state.added.remove(record)
    } else {
        record.quantity--;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question