The error message "TypeError: Unable to access the 'get' property of an undefined vue js object" appeared

Struggling to grasp the concept of vue.js, I am currently navigating my way through understanding how to fetch or call an API. Setting up my index.html and app.js, along with the required packages in the node_modules, has been relatively smooth.

However, upon running my file, I encountered a frustrating

TypeError: Cannot read property 'get' of undefined
error on the console. The solution provided was to install vue-resource and include the two lines of code below. But where exactly should I insert them? Inside the app.js file itself? Apologies for my lack of knowledge; I'm still fairly new to Javascript and Vue.js.

var Vue = require('vue');
Vue.use(require('vue-resource'));

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <script src="https://unpkg.com/vue" ></script>

    <title>article-app</title>
  </head>

  <body>
    <div id="vue-app">
      {{ articles }}
    </div>

    <script src="app.js"></script>
    <script src="main.js" ></script>
  </body>
</html>

var article = new Vue({
    el: '#vue-app',

    data: {
        articles: ''
    },

    created: function () {
        this.fetchData();
    },        

    methods: {
        fetchData: function () {
            var that = this
            this.$http.get('http://localhost/aim-beta/rest/export/json/article'),
                function (data) {
                    this.articles = data.main.temp;
                }
        }
    }

});

Answer №1

Set up vue-resource in a separate file called plugins/vue-resource.js

const Vue = require('vue');
const Resource = require('vue-resource');

Vue.use(Resource);

Next, include the file in your app.js, like this:

require('./plugins/vue-resource.js');

new Vue({
  el: '#vue-app',
  data: {
    articles: ''
  },
  created: function () {
    this.fetchData();
  }, 
  methods: {
    fetchData: function () {
      this
        .$http
        .get('http://localhost/aim-beta/rest/export/json/article',
          function (data) {
            this.articles = data.main.temp;
        })
    }
  }
});

As a result, your updated folder structure will look like this:

- index.html
- app.js
+ plugins
  - vue-resource.js

Using the native JavaScript Fetch API

// No need for vue resources!!!
// require('./plugins/vue-resource.js'); 
// Learn more about Fetch API: [https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch][1]

new Vue({
  el: '#vue-app',
  data: {
    articles: ''
  },
  created: function () {
    this.fetchData();
  }, 
  methods: {
    fetchData: function () {
      fetch('http://localhost/aim-beta/rest/export/json/article', {
        method: 'GET'
      }).then(function(data) {
        console.log(data);
        this.articles = data.main.temp;
      })
    }
  }
});

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

Gather information from various mongodb collections and consolidate them into a single array using Javascript

Is there a way to retrieve the most recent item from multiple collections and store them in an array based on the collection name and newest item? How can I accomplish this either sequentially or asynchronously? let dat = ["test", "test2"]; ...

Achieving success in element-ui vuejs involves validating the state with the :error parameter

I recently developed an app using Laravel, Vuejs, and Element-ui. Within my form, I have utilized the "error" property to indicate that Laravel validation is in place. <el-form-item label="First Name" prop="firstname" :error="registerForm.errors.get(&a ...

Tips for concealing content within table cells

I am facing an issue with my form that contains a table. When the user clicks on the radio button labeled "No", I want the content of the subsequent cells in that row to become visible. However, when they click on "Yes", the content should be hidden again. ...

How can Node.js and Express be used to conceal Javascript code on the backend?

I'm a beginner when it comes to Node and Express. I have a query regarding how to securely hide Javascript code on the backend. Currently, I am working with Node.js and Express. My goal is to prevent users from easily accessing the code through browse ...

Getting hold of HTML elements in a div on a different page

Recently diving into the world of html/javascript, I find myself engaged in a project where I'm loading an external html page within a div. The loaded content looks like this: <div class="content" id="content"> <object ty ...

Tips on redirecting a user to a specific page after they have made a selection without doing so immediately

I am a beginner in the world of coding. Currently, I am working on creating templates for an online software application. Users will have the option to select from different choices using radio buttons. However, I want to ensure that users are not redirect ...

The JavaScript file is not compatible with Internet Explorer 9

My website's JavaScript functions normally work on all browsers except for Internet Explorer 9. In IE7 and IE8, they also work normally. After extensive testing, I have concluded that the JS file simply does not work in IE9, but why is that? The curio ...

Execute a Node.JS query using an HTML select dropdown

My goal is to customize queries to a mySQL database based on the user's selection from select options on my website. Here is the HTML code: <select id = "year"> <option value = "yr" selected>Choose a Year</option> <option id = " ...

Is it possible to implement unplugin-vue-components in Quasar-CLI with the Vite version?

I previously utilized this package with the Vite plugin of Quasar, but now I need to transition to its Vite CLI. I attempted to follow the suggested steps like so: //quasar.conf.js vitePlugins: [ [ "unplugin-auto-import/vite", { ...

The JavaScript fetch API failed to receive a response after sending data via a 'POST' request to a Django server

Currently, I am in the process of developing a poll application that utilizes both Django and React. My approach involves using the fetch API to send POST requests to my Django server and receive detailed information in return for further processing. For a ...

Tips for keeping the header and footer in a fixed position at the top and bottom

Struggling to get my footer to stay at the bottom of the page without causing issues for my content. Tried two methods, but encountering problems with both. How can I resolve this and ensure my content is not affected? Attempted using a custom class "foot ...

How does Node.js contribute to the MEAN stack ecosystem alongside JavaScript and Express, in contrast to Django and Python?

Having experience in developing web applications with Python, Django, and PostgreSQL, I have now shifted my focus to JavaScript and the benefits of the MEAN stack. MongoDB serves as the database for MEAN, similar to how PostgreSQL is used with Python. Expr ...

What is a way to adjust the width of fixed columns in a Bootstrap 4.0 responsive table?

I've been struggling to make this work despite following similar questions and answers. I attempted to adjust the column width by directly setting each one, but nothing seems to change! <thead style="white-space: nowrap"> ...

Passing a JavaScript object that may be undefined to a pug template in Node.js

My journey requires transferring a set of JavaScript objects to the pug template. router.get('/edit/:itemObjectId', async function(req, res, next) { var itemObjectId = req.params.itemObjectId; var equipmentCategoryArr = []; var lifeE ...

Modify the state from a listener that is enclosed in the useEffect function

Within my hook called useQueryEvents, I have a setup that involves fetching all past transactions for a user and listening to the network for incoming/outgoing transactions. These transactions are then passed into a function called addActionToActivity, whi ...

How to retrieve the query string in VueJS

Recently, I've delved into the world of VueJS and so far, it's been a great experience! :) One thing I'm trying to figure out is how to save querystring values to a VueJS variable. It seems like a straightforward task in handlebars + express ...

Tips for creating equal height columns in Bootstrap 4

Working with Bootstrap 4 Beta in an attempt to create two columns that maintain full height regardless of screen size. The code below functions perfectly, ensuring both columns maintain full height across all devices. However, upon inserting the bootstra ...

What strategies can I implement to prevent the JavaScript CallStack from becoming overloaded?

The code snippet below outlines the functionality achieved through JavaScript (specifically for a node.js COMET application): A request is sent to the server and held until there is a response. Upon receiving a response, the data is processed, and anothe ...

Using AJAX to send data to the server in jQuery

Currently, I have an AJAX request implemented on my webpage. I am exploring methods to detect when an AJAX call is initiated within the page using jQuery or JavaScript. Is there a way to identify or trigger a function upon the initiation of an AJAX reques ...

The Art of Validating Forms in Vue.js

Currently I am in the process of developing a form with validation using Vue, however, I've run into some errors that are showing up as not defined even though they are currently defined. HTML <form class="add-comment custom-form" @submit="checkF ...