An error occurred: Cannot access the 'splice' property of an undefined value

//Screenshot

<div  v-for='(place) in query.visitPlaces' :key="place.name">

            <div class="column is-4 is-narrow">
            <b-field label="Nights">
            <b-input type="text" v-model="place.nights" placeholder="e.g. Bandra" required></b-input>
            </b-field>
            </div>

         <div class="card hotel-card" v-for='(hotel, index) in place.hotels' :key="hotel.name">
<header class="card-header">
      <b-tooltip label="Delete Hotel" position="is-left">
     <a  class="card-header-icon" aria-label="Delete Hotel" @click="removeHotel(index)">
                            <span class="icon">
                               <i class="far fa-trash-alt"></i>
                            </span>
                         </a> 
                      </b-tooltip> 
                 </header>
               </div> </div>

// JavaScript

 removeHotel(index) {
          console.log(index);
          this.query.hotels.splice(index, 1);
          //this.priceList = {};
        },

Uncaught Type Error: Cannot read property 'splice' of undefined.

This error persists. Any assistance would be greatly appreciated.

Answer №1

When this.query.hotels is undefined, an error occurs. To prevent this, consider adding an if condition as shown below:

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

Answer №2

When the function was called, you expected this.query.hotels to be an array, but it was not defined as one.

To determine if a variable is an Array, you can use the built-in .isArray method

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Identify alterations in an input field after selecting a value from a dropdown menu

Is there a way to detect changes in the input field when selecting a value from a drop-down menu, similar to the setup shown in the image below? html: <input type="text" class="AgeChangeInput" id="range"/> js:(not working) <script> $(docume ...

"Utilizing Node.js to access modules with their absolute

My directory structure is as follows: -- app/ |- models/ |- user.js |- config.json I am trying to make my user.js file require the config.json. Currently, I am using require('/config') but it doesn't seem to be working. Can som ...

When I apply properties in JavaScript, I find that I am unable to utilize the CSS "hover" feature

For a personal project I am currently working on, I decided to experiment with setting background colors using Javascript. However, I have encountered an issue where my CSS code that changes the hover property does not seem to work after implementing the J ...

Using the Table-multiple-sort feature in boostrap-table is not functioning properly when there are multiple tables present on a single page

I have implemented bootstrap-table along with the extension table-multiple-sort. The issue I am facing is when I include two tables on a single page (with the second table within a modal window), the multisort feature does not seem to work on the second ta ...

Top solution for preventing text selection and image downloading exclusively on mobile devices

My plan is to use the following code to accomplish a specific goal: -webkit-touch-callout:none; -webkit-user-select:none; -khtml-user-select:none; -moz-user-select:none; -ms-user-select:none; user-select:none; -webkit-tap-highlight-color:rgba(0,0,0,0); I& ...

Break down a data structure into a type that includes multiple options

Is it possible for Typescript to support type or interface "destructuring" (if that's the right term)? I am attempting to achieve something similar to the code snippet below: type SomeRandomType = { propertyOne: string; propertyTwo: number; ...

What is the best method for breaking down this JSON data into distinct 'objects'?

I am struggling with deserializing JSON data into multiple instances of an object I have defined. Here is how the object is structured: function Question_Value__c () { this.Id=null; this.Name=null; this.Question__c=null; this.Order__c=null ...

Combining two kebab-case CSS classes within a React component

import React from 'react'; import styles from './stylesheet.moudle.css' <div className={styles['first-style']} {styles['second-style']}> some content </div> What is the correct way to include styles[&ap ...

Clickable element to change the display length of various lists

I am working on a project where I have lists of checkboxes as filters. Some of these lists are quite long, so I want to be able to toggle them to either a specified length or the full length for better user experience. I have implemented a solution, but th ...

Tips on displaying the menu only when scrolling through the webpage

Currently, my website has a right menu that causes overlap with the content on mobile and small devices. To address this issue, I am working on hiding the right menu when the page is stable. The plan is for the menu to appear as soon as the user starts sc ...

Node Express JS: Efficiently handling multiple fetch responses before sending data to client

My goal is to call an API that only accepts one animal name at a time, but I receive the names of multiple animals in a query separated by commas. To achieve this, I plan to call the API once for each animal, push the JSON data into an array, and then resp ...

Run a JavaScript function in 5 seconds

I'm looking to execute this function after a 5-second delay: $(document).ready(function() { $("#signInButton").trigger('click'); }); Your assistance is greatly appreciated. ...

Mastering the Art of Broadcasting Routes in Vue 3 SPA with Laravel Sanctum

I'm currently configuring private-channel broadcasting using my vue 3 single page application in conjunction with a laravel sanctum backend. The broadcasting feature works fine when used without any authorization (for non-private channels), but as soo ...

Creating an infinite scroll with a gradient background: a step-by-step guide

I am currently working on a project to develop an infinite scrolling webpage with a dynamic gradient background that changes based on the user's scroll position. While researching, I came across some code for infinite scrolling using time and date. I ...

Display buttons when hovering with React

Seeking assistance with implementing functionality in a React application where buttons for editing and deleting display only when the mouse hovers over the corresponding row. Currently, the implemented code displays these buttons in all rows on hover. Sn ...

Performing various calculations using a v-for loop to determine multiple totals

I have a unique scenario where I am using nested v-for loops in Vue to display information about users and their accumulated leave. Here is a simplified version of what I am trying to achieve: v-for user in users //Display user's name v-for ...

What is the best way to access the display property of a DOM element?

<html> <style type="text/css"> a { display: none; } </style> <body> <p id="p"> a paragraph </p> <a href="http://www.google.com" id="a">google</a> &l ...

Tips for fixing the issue of "The use of getPreventDefault() is outdated. Please use defaultPrevented instead."

When attempting to fetch data for a specific user from an SQL Server database using JSON data, I encountered an error message in the console: "Use of getPreventDefault() is deprecated. Use defaultPrevented instead." Additionally, the values are not bei ...

Why aren't the divs appearing on the page?

I have developed a JavaScript and PHP page where the script in the PHP page sends data to my SQL database. However, the result is not displayed on my home page. Here is the code snippet: function getVote(question_ID) { var option_ID = document.queryS ...

Vue - Utilizing child slots in the render method

Within my component, I am working with a default slot and attempting to enhance the layout by wrapping each item in the slot within a div. However, I am facing an issue where I need to retrieve the classes of one of the slot elements, but the VNode element ...