vue findIndex not a function

here I got an error in the findIndex() function

Uncaught (in promise) TypeError: state.carts.findIndex is not a function

cartId has rowId and qty and sizeVal and image

Methods

    updateCart() {
        this.$store.dispatch('updateCart', this.cartId)
    }

State

state: {
    carts: [],
},

Vue Inspect Carts

031e0275ef3746070e80d81199bd2580:Object {
    id:1
    name:"T-Shirt"
    options:Object {
        image:"products\July2018\4TXSMgf6eAdrOlaxAMiX.jpg"
    }
    size:"l"
    price:551
    qty:2
    rowId:"031e0275ef3746070e80d81199bd2580"
    subtotal:1102
    tax:115.71
}

mutations

    updateCart(state, rowId) {
        const index = state.carts.findIndex(cart => {cart.rowId == rowId});
        // console.log(index)
            state.carts.splice(index, 1, {
                'qty': cart.qty,
                'size': cart.sizeVal,
            })
    },

In my Action is work

Actions

    updateCart(context, cart) {
        return new Promise((resolve, reject) => {
            axios.patch(`update/cart/${cart.id}` , {
                id: cart.rowId,
                qty: cart.qty,
                size: cart.sizeVal,
                image: cart.image
            })
            .then(response => {
                context.commit('updateCart', response.data)
                resolve(response)
            })
            .catch(error => {
                reject(error)
            })
        })

    },

Now where the error I need to update carts in state.carts need help please guys about it why it gives me the error

Thank you

2 answers

  • answered 2018-07-11 04:16 ittus

    Your state.carts is an object, not an Array. findIndex is an array method

    The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.

  • answered 2018-07-11 05:06 D.jennis

    findIndex is ES6 array feature, make sure it's supported by your browser, or you should apply polyfill or transpile plugins like babel.js to it.