Issue with VueJS rendering data within a for loop

As a newcomer to VueJS, I appreciate your patience as I navigate through this. Let me provide as much detail as possible.

I am currently working on a Vue app that needs to retrieve a response from a server, iterate through the data, and set a Vue data variable with the values.

The issue arises when I attempt to loop through the data using a for loop. While I can access and modify the Vue data within the loop, it does not reflect in the rendering. More details are provided below:

Here is a snippet of my Vue app:

var app = new Vue({
    el: '#app',
    data() {
        return {
            items: {}
        }
    },
    mounted() {
        //using Axios to post -> this all works
        axios({
            method: 'post',
            url: 'fetchOrder.php'
            //used both arrow and normal function still doesn't work
        }).then((response) => {
            response = Object.entries(response.data)
            for (i = 0; i < response.length; i++) {
                //values are all correct
                this.items[response[i][0]] = response[i][1]
            }
        })
    }
})

I have searched extensively for solutions and found that while I am able to assign values to this.items outside the for loop, any modifications made inside the loop do not render on the list (which is displayed using v-for="order in orders"). This has been functioning properly until now.

Oddly enough, when checking the Vue extension, the orders object contains the correct values, indicating that they are being set correctly. The issue seems to lie in VueJS not rendering the updated list. (Refer to my Vue plugin output image below)

Vue plugin data output - image

At this point, I suspect there may be a scope problem where I cannot reference 'this'. I have attempted using .bind(this) without success.

Thank you for taking the time to assist :)

NOTE: It's worth mentioning that I am including Vue via CDN rather than the actual package.

NOTE: The mentioned typo was present on stack overflow but did not resolve my issue.

Answer №1

Recent Development: I managed to resolve my problem by using this.$set(this.items, response[i][0], response[I][1]).

Kudos to @Hismanshu for providing such a helpful solution!

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

Export hashtags from a JSON file to a CSV file in Python

Hello everyone, I recently started coding and I'm working on a project with the code below: f = open("singletweetwithtimezone.json", "r") tweet_text = f.read() import json tweet_json = json.loads(tweet_text) g = open("Singletweetcsvoutput.csv", "w ...

Display a loading dialog for several asynchronous requests being made via AJAX

When making two asynchronous ajax calls, a loading dialog box is displayed for each call using the code below: jQuery('#msg_writter').show(); After a successful request, the loading dialog is hidden with the following code: jQuery('#msg_w ...

Firefox returns a "null" error when attempting to access the Vimeo API, whereas there are no errors reported when accessing the YouTube API or when

I tried researching this issue on multiple platforms, including Google and other forums, but I am still unable to resolve it. I recently transitioned from a proxy request approach (using AJAX to communicate with a server-side script that then interacts wit ...

Error message: Unhandled error - $(...).sidr does not exist as a function. [Chrome developer console]

I included this code in the basic module HTML block of a WordPress page builder and encountered the white screen of death. According to the Chrome developer console, the following error occurred: helpers.js?ver=4.5.3:15 Uncaught TypeError: $(...).sidr is ...

What is more effective: utilizing document fragments or string concatenation for adding HTML to the DOM?

My task involves adding a collection of cards to the DOM. html <div class="card"> <div class="card-title">Card 1</div> <div class="card-subtext">This is card 1</div> </div> js let ...

Enhance the appearance of Google Maps by incorporating gradient effects, box shadows, or other similar visual

How can I add a linear gradient to Google Maps? Below is my code snippet. HTML <div id="map"></div> CSS #map{ height:100vh; background: linear-gradient(90deg, #f7f8fa 0%, rgba(247, 248, 250, 0) 18.73%), linear-gradient(180deg, # ...

Maintain connection on a specific route rather than across all routes within the router-view

When specifying keep-alive to router-view like this: <keep-alive> <router-view></router-view> </keep-alive> All routes are cached and reloaded whenever that route is revisited. I am interested in having the option to specify ...

In order to display the particle-slider logo effect, the JavaScript on the page needs to be refreshed

I have a website frontend integrated from WordPress using an HTML 5 Blank Child Theme. The site features a logo effect utilizing particle slider for screen sizes greater than 960px, and a flat logo image for screen sizes less than 960px. Everything works p ...

Utilizing web components from NPM packages in conjunction with Svelte

My current project involves the development of a simple Single Page App (SPA) using Svelte. I have successfully implemented a basic layout and styling, as well as an asynchronous web request triggered by a button click. Now, my next objective is to utiliz ...

"Troubleshooting the issue of a null object when sending JSON to a WCF Rest Service

I am new to using REST, WCF, and JSON in my application and I'm having trouble with the 'POST' functionality. While the 'GET' method is working fine, the 'POST' method is giving me issues. After packing up my JSON data u ...

Using jQuery's slideToggle feature to hide content when clicked again or when a different button is

I have a challenge with creating toggle buttons that require specific functions: 1) Display content on the first click (this is working) 2) Conceal content when another button is clicked (this is also working) 3) Hide content on the second click (this i ...

JavaScript for loop similar to Python'sIn JavaScript, the

As someone who is new to programming, I primarily use Python but have now encountered a situation where I need to work with JavaScript for a project utilizing Phonegap. The query retrieved from the server looks like this: [["CompanyName", "lat", "long", ...

Error: The function cannot be performed on _nextProps.children

I'm having trouble implementing react context with nextJS and I keep encountering this error: Server Error TypeError: _nextProps.children is not a function This is my code for _App.js: import Head from "next/head"; import Router from &q ...

Working with both types of JSON data?

I am currently developing an application using Go that will be receiving two different formats of JSON data: For instance 1: {"book_data":{"title":"book-title","page_number":457}} For instance 2: {"book_data":{"collection":214},"books":{"data":[{"title ...

What could be causing my prompt for a username to not work properly as I attempt to develop a system that generates a unique code using the user's initials?

I'm currently working on a script that will be used by the secretaries in our school district to generate unique codes for new students or hires. The code will consist of an 8-digit output, a randomly generated 6-digit number, and the user's init ...

Can you elaborate on the contrast between React Server Components (RSC) and Server Side Rendering (SSR)?

I'm curious about the differences between RSC in React 18 and SSR in NextJS. Can anyone explain? ...

What is the best method to organize HTML tables in Knockout by utilizing a Breeze navigation property?

Currently, I am utilizing Breeze.js to manage my data model within Knockout.js. This involves a simple view model with an adapter library for handling sorting tables on entity properties. The tables are sorted using tablesorter, along with a knockout bindi ...

What is the best way to transfer data between controllers in my particular situation?

Currently, I am working on developing a factory for the restful services. The main challenge I'm facing is how to transfer data from one controller to another in AngularJS. Specifically, I need to use the data obtained from the first service call to ...

bespoke theme background hue

I currently have material-ui@next installed and I am attempting to customize the background color of the theme. Here is what I have tried: const customizedTheme = createMuiTheme({ palette: createPalette({ type: 'light', primary: purple ...

Troubleshooting: The issue with json_encode in Ajax calls

I am facing an issue with my ajax call and the json response. The console is indicating that my php file is not returning a json format, but I am unable to pinpoint the exact reason behind it. Below is my ajax function: function showEspece(espece, categori ...