0

I'm having some trouble trying to access an API to get or fetch data. I'm still currently new to vue.js and javascript. I'm getting an error Uncaught SyntaxError: Invalid shorthand property initializer. I can't seem to understand what the error means or seems to indicate.

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

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

    data: {
        articles = ''
    },

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

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

});

4 Answers 4

1

Instead of using this.$http, use axios library for making api calls.

Sign up to request clarification or add additional context in comments.

2 Comments

I've just installed axios on dev my folder. Where do I usually place this three lines import Vue from 'vue';, import axios from 'axios';, import VueAxios from 'vue-axios';? Do include them inside my app.js?
can you create a plunker or fiddler for this. it would be easy to fix there.
0

I think you can't use equal in the JS object syntax

data: {
    articles = ''
}

1 Comment

Right, I didn't notice that. Now I'm currently getting an error Cannot read property 'get' of undefined
0

Try

data: function() { 
    return () {
        articles: ‘’
    }
}

And specify http:// to the localhost

this.$http.get('http://localhost/aim-beta/rest/export/json/article'),
            function (data) {
                this.articles = data.json()
            }

1 Comment

Hi, I'm getting an error "TypeError: Cannot read property 'get' of undefined"
0

Use this for the data JSON object:

data: {
     articles: ''
}

Then, use Promise for firing the HTTP request (note that I used the http:// with the URL):

this.$http.get('http://localhost/aim-beta/rest/export/json/article')
          .then(function(response){
                this.articles = response.json();
          });

Source : Documentation

6 Comments

Hi, I'm getting an error TypeError: Cannot read property 'get' of undefined
try using Vue.http.get instead of this.$http.get. Then, let me know.
Hi, Same error on the console TypeError: Cannot read property 'get' of undefined
Use this: before creating your Vue app, add this line before -> Vue.use(VueResource) Make sure you import the vue-resource : import VueResource from 'vue-resource'
Are you using any bundlers like webpack or browserify? Then, the above comment should work (Source : Config Docs)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.