Lewis Menelaws
Lewis Menelaws

Reputation: 1186

VueJS api call leaving data undefined

I am learning VueJS 2.0 and I am connecting to an API where I want the value of some data to change on input change. Here is what the output says using the dev tools:

canadianDollar:undefined europeanEuro:undefined europeanPound:undefined usd:"1232"

Whenever I put 1232 in the USD input field it doesn't return anything and leaves those properties as undefined. Here is the code.

new Vue({
  el: '#app',
  data: {
    usd: '',
    canadianDollar: '',
    europeanPound: '',
    europeanEuro: ''
  },

  // Watch methods
  watch: {
      usd: function() {
          this.convertCurrency()
      }
  },

  // Logic Methods
  methods: {
    convertCurrency: _.debounce(function() {
        var app = this;
        if (app.usd.length !== 0) {

            // Show that the things are loading in.
            app.canadianDollar = 'Searching...'
            app.europeanPound = 'Searching...'
            app.europeanEuro = 'Searching...'
            console.log(app.usd)
            axios.get("http://api.fixer.io/latest?base=USD&" + app.usd)
                .then(function(response) {
                    app.canadianDollar = response.data.CAD
                    app.europeanPound = response.data.GBP
                    app.europeanEuro = response.data.EUR
                })
                .catch(function(error){
                    app.canadianDollar = "ERROR"
                    app.europeanPound = "ERROR"
                    app.europeanEuro = "ERROR"
                })
        }
    }, 500)
  }
})

and the HTML:

<!DOCTYPE html>
<html>
<head>
  <title>Welcome to Vue</title>
  <script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <input type="text" name="" value="" v-model="usd">
    <ul>
        <li>Canadian Dollar: {{canadianDollar}}</li>
        <li>European Pound: {{europeanPound}}</li>
        <li>European Euro: {{europeanEuro}}</li>
    </ul>
  </div>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js" charset="utf-8"></script>
  <script src="index.js" charset="utf-8"></script>
</body>
</html>

When I type in a number it does give me the "Searching" part but disappears and nothing shows up.

Upvotes: 0

Views: 703

Answers (2)

Frazer Kirkman
Frazer Kirkman

Reputation: 1154

I would recommend changing

then(function(response) {
   app.canadianDollar = response.data.CAD
   app.europeanPound = response.data.GBP
   app.europeanEuro = response.data.EUR
})

to

then(function(response) {
   console.log(response);
})

that was you can see what is being returned.

Also, axios.get("http://api.fixer.io/latest?base=USD&" + app.usd) should probably have a name like vulue:axios.get("http://api.fixer.io/latest?base=USD&VALUE=" + app.usd), but you'll have to check their api to see what it is meant to be called.

Upvotes: 1

William Valhakis
William Valhakis

Reputation: 370

...

response.data.rates.CAD;

you have

response.data.CAD;

...

app.canadianDollar = response.data.rates.CAD * app.usd;

Upvotes: 0

Related Questions