Home > Enterprise >  Axios "TypeError: Cannot read properties of undefined (reading 'then')" on reque
Axios "TypeError: Cannot read properties of undefined (reading 'then')" on reque

Time:02-19

I'm trying making a GET request using Axios to my Discord bot, but i'm getting a problem.

var axios = require("axios");
var response;

function getItemPrice(name, time) {
  axios.get("https://url.com/api/id=${name}&time=${time}")
}

response = getItemPrice("name", "time");

response.then(function(dataResponse){
  console.log(dataResponse.data);
}
).catch(function(error){
  if(error){
    console.log(error);
  }
});

I changed the URL. Show this in console ↓

TypeError: Cannot read properties of undefined (reading 'then')

CodePudding user response:

You have to use backticks (``) in URL to use template literals. And also use return from function getItemPrice.

Example:

axios.get(`https://url.com/api/id=${name}&time=${time}`)
  • Related