7

Trying to send an http get request using Vue js. Can't see any problems with the logic, not too experienced using vuejs though.

Keep getting these two errors:

[Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'get' of undefined"

and

TypeError: Cannot read property 'get' of undefined.

var owm = new Vue({
  el: '#owm',
  data: {
    debug: true,
    weather: []
  },
  methods: {
    loadWeather: function() {
      this.$http.get('http://api.openweathermap.org/data/2.5/find?q=stockholm&type=like&appid=766b78c39446a8fa6313c3b7b2063ade', function(data, status, request){
        if(status == 200)
        {
          this.weather = data;
          console.log(this.weather);
        }
      });
    }
  },
  mounted: function () {
    this.loadWeather();
  }
});

Updated the code using vue resource, the errors are gone but it won't console log any data, what could be wrong?

Vue.use(VueResource);
var owm = new Vue({
  el: '#owm',
  data: {
    weather: []
  },
  methods: {
    loadWeather: function() {
      this.$http.get('http://api.openweathermap.org/data/2.5/find?q=stockholm&type=like&appid=[API KEY]', function(data, status, request){
        if(status == 200)
        {
          this.weather = data;
          console.log(this.weather);
        }
      });
    }
  },
  mounted: function () {
    this.loadWeather();
  }
});

EDIT: This code works, don't really understand the .then function though and why the request won't work with the callback function but the .then function does.

this.$http.get('http://api.openweathermap.org/data/2.5/find?q=stockholm&type=like&appid=[API KEY]').then((data) => {
  this.weather = data;
  console.log(this.weather);
3
  • 1
    check this, stackoverflow.com/questions/42387414/… Commented Nov 21, 2017 at 9:04
  • But do not use vue-resorce package. It is deprecated now. Read this about alternatives: medium.com/the-vue-point/retiring-vue-resource-871a82880af4 Commented Nov 21, 2017 at 9:27
  • installed vue resource and running : var Vue = require('vue'); Vue.use(require('vue-resource')); in the code, now i get the error saying "require is not defined" Commented Nov 21, 2017 at 9:31

1 Answer 1

8

I tried a sample on my machine .you are using $http in wrong way. refer the docs.Since $http resolves a promise its callback can be handled inside a then function. This worked for me:

 loadWeather: function() {
    var self=this;
  this.$http.get('http://api.openweathermap.org/data/2.5/find?q=stockholm&type=like&appid=766b78c39446a8fa6313c3b7b2063ade').then(function(response){
    if(response.status == "200"){
        console.log(response);
    self.weather = response.data.list[0].weather // use self instead of this
    }


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

4 Comments

Still not getting any data
Are you able to make the request successfully?? or do you get any error while sending the request ? can you send the screenshot of response from network panel in debug tool.
Getting no errors with the request, but no data returned either
Ran your code, still not returning any data. This code works for me though this.$http.get('api.openweathermap.org/data/2.5/find?q=' + this.userInput + this.apiKey).then((data) => {}, but not familiar with what the .then function does.

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.