I've a problem with a return data from an async function located in Controller file.
I would to get my data in "let data", but it is undefined, I can't understand where is my error..
(of course in my concept of async functions :) )
Here my example:
// index.js
const DataController = require('../controllers/DataController');
router.get('/test', function (req, res, next) {
let data = DataController.getData().then((resp) => {
console.log(resp); // <-------- here is undefined
});
});
// DataController.js
const axios = require('axios').default;
exports.getData = async function getData() {
return axios.get("https://it.lipsum.com/")
.then((response) => {
// console.log(response)
return response;
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
}