Error: The Vue component's property is not defined

Recently, I started learning Vue. One issue I encountered is with an AJAX request that updates an object used in my Pug template at intervals. Strangely, while referencing the data using double curly brackets works fine on its own, it becomes undefined and breaks the component when used within a loop like forEach.

<template lang="pug">
.mycomponent
  p {{ data }}  <- this reference to data works fine
  if data != undefined <- this reference to data is undefined
    each val, index in data
      li= val + ': ' + index
</template>

To update the data, I'm trying:

mounted () {
  setInterval(() => {
    axios.get('http://localhost:3000/api/info')
      .catch((error) => console.log(error.request))
      .then(response => {
        this.data = response.data
      })
  },
  3000)
},

Can someone help me understand why 'data' is becoming undefined?

Answer №1

Here is a sample of Pug template code:

<template lang="pug">
.mycomponent
  p {{ data }}
  ul(v-if="data !== undefined")
    li(v-for="(val, index) in data") {{ val }} : {{ index }}
</template>

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

What is the best way to handle a request within an AngularJS $httpProvider interceptor?

It appears that there may be a simple solution I'm overlooking here. I am interested in developing an interceptor for the $httpProvider to inspect outgoing $http requests and handle certain cases differently, such as those targeting non-existent endp ...

Cordova - everything shifts to the left

Hi there, I'm having an issue with cordova on android versions 4.4 and 4.3 where it seems like all objects are shifting to the left: https://i.sstatic.net/ExjKB.jpg However, on android 5.0 it looks like this: https://i.sstatic.net/tZWZd.jpg Here i ...

What is the best way to prevent a draggable div from moving past the edge of the viewport?

My situation involves a draggable div that functions as a popup window when a specific button is clicked. However, I noticed that dragging the div to the end of the viewport allows me to drag it out of the visible area, causing the body of the page to expa ...

Using Google Maps API to combine geoJSON properties with fixed text in an infobox

I've been working on a GoogleMaps project where I load geoJSON points and display their "waterlevel" property in an info-box when hovering over a point. However, I want the info-box to show "Water Level = #" instead of just "#". So far, I've man ...

creating links using json2html

I need assistance to create a clickable anchor using json2html with the following transformation: 'renderTimeline':[ { tag: "a", class: "btn btn-warning btn-circle", style: "float: right;", html: "<i class=\"icon-rem ...

"Encountering an error with the any type in the useLocation feature while using React Router version 6

https://i.sstatic.net/0YcS9.png What steps should I take to resolve this particular type of error issue? My attempt at passing a custom type did not yield successful results. ...

The option value in mat-autocomplete is not displaying correctly on IOS devices

When I click on the first option in the dropdown menu, it does not display the selected option in the field. However, when I select the second option, then the value of the first option appears, and when I choose the third option, the value of the second o ...

innerhtml does not display the entire array contents

I'm facing an issue with my innerHTML output where it seems to be displaying one less value than expected from the array. I've been unable to pinpoint the exact problem. await fetch(urlbody, bodyrequestOptions) .then((response) => response ...

Press the button to update several span elements

Imagine I have multiple span elements like this: <span>A</span> <span>B</span> <span>C</span> <span>D</span> and a div element (which will be converted to a button later) named "change". <div id="chan ...

Display the checkbox as selected using AngularJS

I have already submitted a form with a checkbox value. Now, I am trying to display the checkbox as "checked" using AngularJS based on the value retrieved from the data source. Can someone guide me on how to achieve this? ...

"Utilize an enumeration to access a certain number by passing

Seeking to retrieve the id based on the animal's name: enum Animals { Cat = 1, Dog, // 2 } const name: string = "Cat"; const id: number = Animals[name] // Element implicitly has an 'any' type because index expression is not of type & ...

What is the best method for incorporating meta data, titles, and other information at dynamic pages using Node.js and Express?

I am currently exploring methods to efficiently pass data to the html head of my project. I require a custom title, meta description, and other elements for each page within my website. Upon initial thought, one method that comes to mind is passing the da ...

The source property in the Nextjs Image tag is not valid

<Image src={"http://localhost:3000/images/img.jpeg"} layout="fill" objectFit="cover" width={"100%"} height={"100%"} /> An error message has appeared: https://i.sstatic.net/jI9mh.png Any s ...

Change the size of the font with a slider in VueJS

I have been utilizing the project found at This particular project allows end-users to reuse Vue components as editable sections by providing a styler overlay for adjusting content within a div or section. After installation, I added a slider which now ap ...

Iterate through the array in order to showcase or output it in PHP syntax sourced from an API

[0] => Array ( [id] => 6 [name] => Digitally Imported Psy Goatrance [country] => GB [image] => Array ( [url] => https://img.dirble.com/station/6/original.png ...

Successful jQuery Ajax request made without the need for JSON parsing

I find it strange that my experience with jQuery's ajax function is completely different from what I'm used to. Below is the javascript code in question: $.ajax({ url: "/myService.svc/DoAction", type: "GET", dataType: "json", su ...

Adjust Bootstrap: apply the class "collapsed" to a different button upon clicking

I am currently working on incorporating multiple collapsible panels into a navbar. I have successfully collapsed panel 1 when panel 2 is open, but I am facing an issue where button 1 does not return to the collapsed state. Is there a way to add the "collap ...

Get information about mailchimp mailing lists using ajax requests with the iron-ajax element in Polymer

Struggling to access the mailchimp API and display list details? Unsure of how to send authentication credentials to retrieve the information you need? Here are a few attempts that have been made so far: <iron-ajax auto url='https://us3.ap ...

Although Sequelize has the capability to generate tables, it lacks the ability to perform insertions or selections of data

Currently, I am in the process of developing an express app that incorporates basic MVC functionality with Sequelize. At this stage, my focus is on creating a route to insert a single row of student data into the database. Upon starting the server, Sequel ...

Is it possible for the 'error' event to be triggered on the 'http.IncomingMessage' object during a node.js http.request?

There are 4 ways to encounter errors when calling http.get(url, cb): httpThrows() This error can occur due to a wrong format of the url or incorrect callback. function httpThrows() { try { http.get("www.missing-protocol.com", res => { ...