0

hello im kinda of a newbie to axios how can i return response.data to the getOrderDetails function ? thanks !

function getOrderDetails(orderid) {
  var axios = require("axios").default;

  var options = {
    method: 'GET',
    url: 'https//example.com'+orderid,
    headers: {
      'Content-Type': 'application/json',
      Authorization: '*secret api key*'
    }
  };

  axios.request(options).then(function (response) {
    console.log(response.data);
      // return this ^^^^^^
  }).catch(function (error) {
    console.error(error);
  });
}
  • 1
    `return axios.request(options).then(res => res.data)` – bill.gates May 23 '22 at 21:35
  • In your example if you simply `return response.dada;` then the value of `response.data` will be the fulfilled result of the Promise object. – David May 23 '22 at 21:42
  • when i try this :return axios.request(options).then(res => res.data) , it gives me a :Promise { } – meshari alzahrani May 23 '22 at 22:02
  • @mesharialzahrani: That is correct and expected behavior. It's an asynchronous operation and it results in a Promise. You would either `await` the Promise or append a `.then()` callback to it. – David May 24 '22 at 11:32

0 Answers0