programing

vue findIndex가 함수가 아닙니다.

goodcopy 2022. 7. 19. 21:37
반응형

vue findIndex가 함수가 아닙니다.

여기서 에러가 발생했습니다.findIndex()기능.

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

cartId에는 rowId, qty, sizeVal 및 이미지가 있습니다.

방법들

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

state: {
    carts: [],
},

Vue 검사 카트

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
}

돌연변이

    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,
            })
    },

내 액션은 일이다

행동들

    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)
            })
        })

    },

이제 state.carts에서 카트를 업데이트하기 위해 필요한 오류는 왜 오류가 발생하는지 설명해 주십시오.

감사합니다.

당신의.state.carts어레이가 아닌 객체입니다. findIndex배열 방식입니다.

findIndex() 메서드는 지정된 테스트 함수를 충족하는 배열 내 첫 번째 요소의 인덱스를 반환합니다.그렇지 않으면 -1이 반환됩니다.

Vue에서 글로벌하게 가져온 혼합으로 PolyFill을 사용합니다.

 const findIndex = (values, expectedValue) => {
    let selectedIndex;
    const valuePresent = values.some((value, index) => {
      selectedIndex = index;
      return value === expectedValue;
    });
    return valuePresent ? selectedIndex : -1;
  };

findIndex는 ES6 어레이 기능입니다.브라우저에서 지원되는지 확인하거나 babel.js와 같은 polyfill 또는 transfile 플러그인을 적용해야 합니다.

언급URL : https://stackoverflow.com/questions/51277081/vue-findindex-not-a-function

반응형