Transferring objects from one array to another in Vue.js

I am facing an issue with two arrays containing custom components.

List A represents search results, while List B holds selected items.

Each component includes a template that displays the array item.

The problem arises when I try to move an item from List A to List B by clicking on a link. Instead of success, I encounter an error message stating

Cannot read property 'push' of undefined

Provided below is my complete Vue code. Can you identify where I may be going wrong?

new Vue({

    el: '#search',
    data: {
        query: '',
        listA: '',
        listB: ''
    },
    methods: {

        search: function(event) {

            if (this.query !== "") {
                this.$http({url: '/list-a?search=' + this.query, method: 'GET'}).then(function(response) {
                    this.listA = response.data
                });
            };

            event.preventDefault();
        }

    },
    components: {
        listaitem: {
            template: '#listaitem-template',
            props: ['lista-item'],
            methods: {
                selected: function(listaitem) {
                    // Upon click, add 'listaitem' to listB
                    this.listB.push(listaitem);
                }
            }
        },

        listbitem: {
            template: '#listbitem-template',
            props: ['listbitem']

        }  
    }
});

Answer №1

Instead of initializing listA and listB as empty strings, it is recommended to use empty arrays like below:

data: {
        query: '',
        listA: [],
        listB: []
}

By doing this, you can safely utilize this.listB.push(listaitem); within the listaitem component without encountering any errors.

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

Why is it that a website is loading at a snail's pace on Angular?

Working on my college project has been going smoothly, except for one issue with slow loading times. It's frustrating how long it takes to load. I suspect that there might be an error in the deployment process or something else causing the delay. The ...

What causes the discrepancy in smoothness between the JavaScript animation when offline versus its choppiness when online, particularly on AWS

Recently I delved into game development using HTML5/CSS/JS and embarked on a small project. Check out the game here at this AWS storage link: If you open the game and press SPACE, you'll notice that the ball starts moving with occasional brief pauses ...

Using a lambda function with skimage.measure.block_reduce

In my data analysis project, I start with an array: import numpy as np arr = np.random.randint(0,10,size=[8,8]) The values in arr are: array([[9, 1, 8, 2, 0, 4], [0, 7, 6, 9, 7, 5], [0, 7, 1, 6, 6, 2], [3, 6, 3, 3, 8, 1]]) To reduce ...

React - error caused by an invalid hook call. Uncaught Error: React encountered a minified error with code #

My goal is to incorporate the micro-frontend concept by implementing various react apps. Container Header Dashboard All three are separate applications. I intend to utilize the Header and Dashboard apps within the Container app. For the Header app, it& ...

Ways to conceal an element when it appears in the initial section of a page

I currently have a single-page website consisting of 4 sections which are not scrollable. To navigate between these sections, I have implemented a burger menu button with a fixed position to ensure it remains visible on all sections except the first one. ...

Explore multiple options and apply filters using Vuex

I've been working on filtering data in my vuex store, but I'm encountering some issues. When I select just one option, it filters and displays the checked item correctly. However, if I check multiple options, it doesn't display anything... ...

The premature reveal of the back side in the Kendo UI flip effect on Internet Explorer

Currently, I am in the process of creating a BI dashboard for a business application using JavaScript Kendo UI version v2014.1.416. Unfortunately, I have encountered an issue with certain visuals while testing on IE11. I must mention that due to practical ...

Making an HTTP request within a forEach function in Angular2

Encountering an issue while using the forEach function with HTTP requests. The _watchlistElements variable contains the following data: [{"xid":"DP_049908","name":"t10"},{"xid":"DP_928829","name":"t13"},{"xid":"DP_588690","name":"t14"},{"xid":"DP_891890" ...

What could be the reason for the absence of this modal?

I'm trying to create a modal that will be shown to users when they use the command modal. Can anyone help me troubleshoot this code? const { MessageActionRow, Modal, TextInputComponent } = require('discord.js'); client.on('interactionC ...

JavaScript code is experiencing issues following a for loop

I recently developed a code to upload resized images using HTML and JavaScript. Here is the HTML form setup: <form action="aa.php" method="post" id="uploadImageForm" enctype="multipart/form-data"> <input type="file" id="fileToUploadId" ...

Choose distinct phrases from a collection

I am facing an issue with retrieving unique text from the database. Let's consider a movie database with 2 movies stored: 1st movie falls under the categories: Drama, Romance, War 2nd movie falls under the categories: Drama, Thriller What I want is ...

Sequelize: Confusion caused by overlapping names for attribute 'playlist' and association 'playlist'?

Encountered an error while using node.js, Sequelize, and MariaDB. The error message states a naming collision between attribute 'playlist' and association 'playlist' on model playlist_entry. Unsure about how to resolve this issue. Er ...

What is the best way to manage the retrieved data in a situation where I am utilizing async-await along with the useEffect hook?

I need to retrieve a player's statistics based on the input provided by the user, and then show it. I am facing challenges in storing the retrieved data using the useEffect hook. Any suggestions for a more efficient approach? import React, { useEf ...

What are the steps to executing a function that instantiates an object?

Here is an object with filter values: const filters = ref<filterType>({ date: { value: '', }, user: { value: '', }, userId: { value: '', }, ... There is a data sending function that takes an obje ...

The readyState remains stuck at 1 without progressing further

Struggling to input data into an XML using javascript's open() function. The website remains stuck at readyState 1, Here is the Javascript code snippet: function addItem() { var name = document.getElementById('Iname').value; va ...

"Transforming the Website Background with a Splash of Color

I am trying to figure out how to change the color scheme of my website with a button click. Currently, when I select a different color, only the background of the webpage I'm on changes, not the entire website. I have applied CSS but it's not ref ...

Looking up results in an array in PHP/MySQL

I have retrieved the results from mysql ($date_options) as shown below: Array ( [0] => stdClass Object ( [id] => 4 [start_date] => 2010-09-29 ) [1] => stdClass Object ( [id] => 13 [start_date] =&g ...

Preserve the content in the text box corresponding to the selected radio button

In my form, there are multiple radio buttons, each with an associated text box value. Users can only select one radio button at a time and submit the form. However, sometimes users will enter data in a textbox for one radio button, then switch to another o ...

Exploring individual elements within an array within a list in C#, a step at a time

I'm currently facing a minor issue. In the final stages of developing a small application, I find myself stuck on deciding the optimal approach to proceed. The functionality of the program involves receiving a string input from the user via a textbox ...

Can JavaScript/jQuery be integrated with MySQL/PHP?

I have a unique question that I've been unable to find an answer to, which may indicate its complexity. The reason for my inquiry is because I am in the process of developing a food delivery website. Within the admin panel of this website, administrat ...