Ajmal Aamir
Ajmal Aamir

Reputation: 1175

axios didn't return data with matching query string of parameter object but returns all the data in vanila JS

i have an api endpoint https://countriesnode.herokuapp.com/v1/countries.

Now i am fetching all the countries by

axios.get('https://countriesnode.herokuapp.com/v1/countries')

it returns successfully. Besides I want to fetch single countries with the country code like 'BB' .and i am trying with params and my object is like below

axios.get('https://countriesnode.herokuapp.com/v1/countries',
 {
 params: {
 code: codeId
 }

but it returns all the data like above rather than showing a single country with the following code. I also want to extract only the currency and area code. I wanted to try like this, don't know it gonna work or not.

.then(axios.spread((object) => {
      console.log('Currency: ', object.data.currency);
      console.log('Area code: ', object.data.emojiU);
    }));

please someone help me... It took me almose the hole day for this but not able to success.

Upvotes: 0

Views: 648

Answers (1)

mgarcia
mgarcia

Reputation: 6325

As per your comment, if you want to get the data of a specific country, you only have to append the country code id to the URL:

const codeId = 'BB';
axios.get(`https://countriesnode.herokuapp.com/v1/countries/${codeId}`)

Would give you the data for Barbados.

If you want to access the currency attribute you can do so with the code:

const codeId = 'BB';
axios.get(`https://countriesnode.herokuapp.com/v1/countries/${codeId}`).then(function(response) {
    const currency = response.data.currency;
});

Upvotes: 1

Related Questions