[Vuejs]-Uncaught TypeError: Cannot read property 'splice' of undefined

0👍

It mean this.query.hotels is undefined. So add if condition like below

removeHotel(index) {
  console.log(index);
  if(this.query.hotels && this.query.hotels.length > 0) {
    this.query.hotels.splice(index, 1);
   }
  //this.priceList = {};
},

0👍

You’ve expected this.query.hotels to be an array but you didn’t define it as one at the time the function was called.

You can test if a variable is an Array by using the static .isArray method

function removeHotel(index) {
    if(Array.isArray(this.query.hotels) && this.query.hotels.length > 0) {
        this.query.hotels.splice(index, 1);
    }
}

Leave a comment