Troubleshooting: Vue JS failing to recognize new objects and properties in HTML

Within my Vue instance, I have a method named `loadPlannedAlerts` that performs an AJAX call to retrieve JSON data. This method is called during the `mounted` lifecycle hook of my Vue JS instance. The retrieved JSON object consists of key-value pairs structured like this: {key: 'value, key2: 'value2}'.

loadPlannedAlerts: function() {
        $.ajax({
            method: 'GET',
            url: '/get-my-data',
            success: function(data) {
                myApp.messages = JSON.parse(data);
                for (var i = 0; i < myApp.messages.length; i++) {
                    myApp.messages[i].findUser = '';
                }
            }
        });
}

In my HTML, I utilize a `v-for` loop to display this message data.

<div v-for="(message, index) in messages">
    <input type="text" name="user" v-model="message.findUser">
</div>

Interestingly, although I can see the added `findUser` key-value pair in my browser's console and Vue plugin dev tools, the data inputted into the message box does not bind correctly to each `findUser` entry.

I am contemplating if there might be any missed details or overlooked order of operations causing this issue. Strangely, existing data binds as expected without any problems after being added.

UPDATE

Upon trying Decade Moon's suggested approach of constructing a regular key-value pair before assigning the value, everything works smoothly. However, when attempting to assign a key-value pair with nested objects holding more key-value pairs, the binding still fails to work.

Answer №1

Have you ensured that myApp.messages is defined at the beginning within the data object of the component?

It seems like you are adding a fresh findUser property to every message. Vue may not be able to detect this change (refer to Change Detection Caveats). It's advisable to modify the payload prior to assigning it to the component in order for Vue to make it reactive.

success: function(data) {
  const messages = JSON.parse(data);
  
  for (var i = 0; i < messages.length; i++) {
    messages[i].findUser = '';
  }

  // Ensure this step is done last
  myApp.messages = messages;
}

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

Tips on how to indicate a checkbox as selected within an Angular controller

I'm in the process of creating a form for entering new service requests, as well as displaying and editing existing ones. One part of this form includes a list of labeled check-boxes that represent different countries. When an existing request is disp ...

Accessing store in Vue, the getter function returns a value representing whether the user is currently logged

I have the user state stored in my Vue store, but when I try to access it like this: let isLoggedIn = store.getters.isLoggedIn Instead of getting a simple true or false, I see this in the console: ƒ isLoggedIn (state) { return state.user ? true : false ...

Styling applied exclusively to the field for mobile devices

As I work on creating a navigation bar using Bulma and Vue.js, I encounter an issue with the dropdown menu behavior. When the navbar collapses into the hamburger menu, the dropdown list remains in display: block; mode. To address this, I attempted a workar ...

There appears to be an issue where the session object cannot be retrieved by the Struts2 action

I have a struts2 action that is invoked by a JavaScript function. The JavaScript function uses uploadify to enable multiple file uploads: <script type="text/javascript"> $(document).ready(function() { $("#fileupload").uploadify({ ...

Difficulty with Horizontal Mousewheel Scrolling

Struggling to implement a horizontal scrolling feature (via mousewheel) for both containers in the code below. I want this feature to be easily applied to any future container creations as well. <body> <style> #container { display: flex ...

Fixing blurry text on canvas caused by Arbor.js mouse event issues

Currently, I am utilizing arborjs in my project. The text within the canvas is created using fillText in html5. While everything functions correctly on a Retina display MacBook, the text appears blurry. To address this, I applied the following solution: v ...

Steps for resetting the Redux Reducer to its initial state

Is there a way to reset a specific Redux Reducer back to its initial state? I'd like the register reducer's state to revert back to its initial state when leaving the register page, such as navigating to the Home or other pages in a React Nativ ...

Searching for occurrences of a specific string within a JSON array

In my database, there is a table named data_table that contains a JSON column called 'data'. The structure of the table appears as follows: select * from data_table; id | data ----+------------------------ 1 | ["a","aa","aaa","a ...

Adding a total row to a Vuetify datatable

Struggling to incorporate a totals row using this framework, but it doesn't seem to be supported out of the box. The workaround I've come up with doesn't quite hit the mark... <v-data-table :headers="headers" :items="desserts" ...

What is the best way to align a modal with a layout when it appears far down the components hierarchy?

Struggling with creating a React modal and facing some issues. Let's consider the structure below: React structure <ComponentUsingModal> <Left> <ButtonToActivateModal> ... </ButtonToActivateModa ...

What is the best way to verify values in Vuejs based on their length?

<button type="submit" :disabled="(user.password && !$v.user.password.valid) || (user.confirmPassword && !$v.user.confirmPassword.sameAsPassword)">sda </button> By implementing a method based on character len ...

Understanding the reverse order of numbers displayed with while loop and innerHTML

function doItAgain() { var loopCount = 5; while(loopCount > 0) { var target = document.getElementById("target"); target.innerHTML = "LoopCount: " + loopCount + "& ...

Nested data selection in React

I have been experimenting with creating a nested select optgroup and attempted the following: const data = [ { sectorId: 5, sectorName: "Sector One", departments: [ { deptName: "Production", jobtitles: [ { ...

Chakra UI: How come the tooltip is appearing in the top left corner of the screen instead of directly above the element?

CreatedByModal is a unique chakra modal that incorporates tooltips. However, I am facing an issue where the tooltip appears at the top of the screen instead of directly above the icon when hovering over the icons. You can see in the image provided that the ...

No data is being retrieved by SWR

I'm struggling to make SWR work in my code. Despite trying multiple examples, I can't seem to get it functioning properly. It's frustrating because the code looks fine and should work. I feel like I must be missing something simple. Current ...

It is not feasible to document JSON within a URL

For my Rest API, I am working on enabling the retrieval of data within a specific bounding box. Since the bounding box requires four coordinates, I intend to structure the GET requests in a way that allows them to accept the bounding box as JSON. This mean ...

Does Chrome have a feature that disables event listeners?

I'm currently working on a tool that incorporates file drag and drop functionality. Strangely, this feature works perfectly in all browsers except for Chrome. Surprisingly, however, it does work in Chrome when the tool is run locally. Here is the cod ...

Exploring the intricacies of extracting nested information from the Rapid API platform

Greetings! I am relatively new to both stack overflow and python, so please forgive me if the formatting is not quite right. Just to note, I am utilizing VS Code. I am currently extracting data from a Covid-19 rapid API, which provides the data in JSON fo ...

Using AngularJS client and Flask server for a RESTful call, one can include the

I am currently facing an issue where I need to send a REST request from my AngularJs client to a Flask server. The problem arises when one of the ids (key) in the request contains a forward slash. Interestingly, if the key does not contain a slash, the re ...

Emulating Data in Angular 2 Using Configuration Similar to Ember Mirage

Is there a way to mock data through configuration in Angular 2 similar to how it's done in Ember Mirage? I'm aware that I can create my own solution using Dependency Injection and MockBackend to intercept HTTP calls and provide dummy data. Howeve ...