[Vuejs]-How to get the response data from API and code the function from data to get the result using node js JavaScript

0👍

The second part of your promise chain will lose the context of this as you are trying to use it here.

                .then(function (data) {
                    console.log(data.rates)
                    for (let [key, value] of Object.entries(data.rates))
                        var jsResult = data.rates;
                    var toCurrency = response.json(this.toCurrency);
                    var fromCurrency = response.json(this.fromCurrency);
                    var fromAmount = response.json(this.fromAmount);
                    var result = this.result;
                    var oneUnit = jsResult.toCurrency / jsResult.fromCurrency;
                    result = (oneUnit * fromAmount).toFixed(2);
                    this.loading = false
                })

You can either redefine this higher in the scope of the convert() function and use the new variable, or adjust your function here to be an arrow function, which will persist the context of this. The latter option is certainly the easier and better one.

.then((data) => {
    ...

You can see more on arrow functions and context here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Leave a comment