Issue encountered while retrieving information using Axios in a Vue.js application

Currently, I am in the process of developing a full stack application using Vue.js and Fastify.js (a Node framework). The Vue app is supposed to retrieve data from the API I created and display it in the browser console. However, I am encountering an issue where the data is not rendering within the app itself. I even attempted to load it using the mount function, but to no avail.

Below is the complete code I am working with:


const app = new Vue({
  el: '#app',
  data: {
    infos: [{
      name: 'Vue',
      age: 19,
      imageUrl: 's3.aws.amazon.com'
    }, {
      name: 'Vue 2',
      age: 20,
      imageUrl: 's3.aws.amazon.com2'
    }],
    msg: 'Hello Vuejs!'
  },
  methods:{
    getData: () => {
      axios.get('http://localhost:3000/getData')
        .then((result) => {
          this.infos = result.data;
          console.log(infos);
        })
        .catch((err) => {
          console.log(err);
        });
    }
  }
})

{{msg}}


Name: {{info.name}}
Age: {{info.age}}
Image: {{info.imageUrl}}

This image displays what is currently being rendered: The default values are shown, and the array is logged on the right.

For the complete code, please visit: https://github.com/siddiquiehtesham/fullstack-vue-nodejs-api

Answer №1

Your axios request is facing an issue where the this keyword is not properly bound to your Vue instance.

To resolve this, you can use arrow functions to maintain the correct context:

getData: function() {
  axios.get('http://localhost:3000/getData')
  .then(result => {
    this.infos = result.data
    console.log(this.infos)
  })
  .catch(err => {
    console.log(err)
  })
}

Alternatively, you can create a self variable before making the request:

getData: function() {
  let self = this
  axios.get('http://localhost:3000/getData')
  .then(function(result) {
    self.infos = result.data
    console.log(self.infos)
  })
  .catch(function(err) {
    console.log(err)
  })
}

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

React Datepicker on Safari: A seamless way to pick dates

While working on my application, I encountered an issue with the Form.Input functionality from Semantic UI React library. I am using it to insert dates and found that it displays a date-picker on Chrome and Firefox but not on Safari. I attempted to use the ...

An issue arises when attempting to fetch data using next.js: a TypeError occurs indicating that

I'm currently working on setting up a next.js website integrated with strapi, but I've encountered an issue with my fetch request. Oddly enough, when I test the request using Postman, the data is successfully returned, indicating that the endpoin ...

Backbone.js encountered an illegal invocation

http://jsfiddle.net/herrturtur/ExWFH/ If you want to give this a try, follow these steps: Click the plus button. Double click on the newly created name field. Enter information into the fields. Press Enter. The era fields (from and until) will be disp ...

How to use Angular 2 to communicate with JavaScript API whenever the router switches to

I am currently working on an Angular2 component that has a template which relies on JavaScript calls to load various components such as Facebook, Google Maps, and custom scripts. The necessary scripts are already loaded in the index.html file, so all I ne ...

Navigating the express subdomain

I'm currently utilizing the package express-subdomain. The issue is that the router handling subdomain requests is the same as the one handling non-subdomain requests. I suspect there's an error in my 'app.js' configuration. How shoul ...

Is it possible to establish multiple connections simultaneously using Stomp?

I have been utilizing the ng2-stomp-service in my Angular application. Is it advisable to set up multiple connections (not just multiple subscriptions)? In the past, I have always seen a single connection being established, with several subscriptions made ...

Operating Laravel-Mix 8 while executing the watch command

How can I successfully load a Vue Component in the Public file using npm run watch? Node.js v14.15.0. npm 6.14.8 Laravel Installer 4.1.0 When running npm run watch in my project directory, I encountered the following error ...

The JSONObject contains a value, however it may sometimes return a null value

[ { "valve": "4679", "selling": "5516", "bal": "9075.4", "o id": "37", "invested": "11122", //<<<<<<<- this value returns null "aProfit": "1012", //<<<<<<<- this value returns null ...

In TypeScript, the catch block does not get triggered

I created a custom pipe in Angular that is supposed to format passed parameters to date format. The pipe contains a try-catch block to handle any errors, but surprisingly the catch block never seems to be executed even when an invalid date is passed. impo ...

The Google Books API has encountered an authentication error with status code 401

Trying to access public data using the Google Books API locally: An error occurred with the authentication credentials. It seems that an OAuth 2 access token, login cookie, or another valid authentication credential is missing. For more information, visit ...

Error encountered while mounting the 'vue-table-2' component in a Vue.js application: "Component mounting failed."

Just starting out with Vue.js and feeling a bit lost... I really want to give 'vue-table-2' a shot after seeing it in action here: (https://jsfiddle.net/matfish2/jfa5t4sm/) but I'm having trouble getting it to work properly. "dependencies" ...

Instructions for creating a distinct click listener for each <img> element inside a table cell

Within my table, each row contains multiple columns with images as data. I successfully implemented a 'common listener' that is triggered when any image within a table row is clicked. $('#mytable tbody td img').click(function () { // ...

Initially, the OWL Carousel image is not displaying correctly due to incorrect sizing

I am currently working with OWL Carousel and have implemented a code that displays one image at a time, with the next image sliding in every 15 seconds. The width is set to occupy 100% of the screen and I have configured the JavaScript accordingly so that ...

Unable to locate the specified view within AngularJS framework

<!doctype html> <html lang="en" ng-app="phonecatApp"> <head> <script src="/js/angular-1.5.2/angular.js"></script> <script src="/js/angular-1.5.2/angular-route.js"></script> <script src="/js/app.js">< ...

Ways to insert text into an SVG file

I am currently using vue-svg-map with a USA map. I am trying to display the state code (path.id) on my svg map. Can anyone provide guidance on how to achieve this? <radio-svg-map v-model="selectedLocation" :map="usa" :location-class="getLocation ...

Maintain text while transitioning to another page

On my webpage (refer to image for details), users need to select options through a series of steps. The process involves clicking on a link in the top box, then entering a search term in the searchbox to display relevant links in div1. Clicking on a link ...

Is there a way to implement multiple "read more" and "read less" buttons on a single page?

I am currently working on a rather large project and I am encountering some issues with the read more buttons. As someone who is fairly new to JavaScript, I am still grappling with the concepts. While I have managed to make the function work for the first ...

Encountering a 404 Error in CKFinder 3 integration with Laravel 6 and Vuejs while accessing the File Browser

My progress so far includes almost being able to successfully upload files to my Laravel installation using CKEditor and the CKFinder file browser opening from the route myurl/ckfinder/browser. However, I have encountered an issue where clicking on the ima ...

What is the process for utilizing the tweed_mode= extended parameter in a Twitter REST API request?

I am in the process of developing a PHP application and sending the following request: $data= $mycon->get("/statuses/show/".$id); The above request functions correctly for tweets that are up to 140 characters and return extended entities if there are ...

Unable to bind click eventListener to dynamic HTML in Angular 12 module

I am facing an issue with click binding on dynamic HTML. I attempted using the setTimeout function, but the click event is not binding to the button. Additionally, I tried using template reference on the button and obtaining the value with @ViewChildren, h ...