Why Axios response doesn't console log result?

Viewed 1583

I'm working on an backend API but at some point I need to get user data from another API. I am trying to use Axios to make http request in order to do that. The request return the result in the browser as expected but the problem is that I can't display console log in the terminal. It doesn't show anything even though I asked the program to do so. Is there a problem probably with my code?

Here is my code :

const axios = require('axios');
const AxiosLogger = require('axios-logger');


const instance = axios.create();

module.exports = (router) => {
    router.get('/profile', function(req, res) {
       //random fake profile info
        axios.get('https://randomuser.me/api/')
            .then(response => {
                console.log(response.data);
                console.log(response.data);
                return response.data
            })
            .catch(error => {
                console.log(error);
            });



       
    });
};
1 Answers

I would suggest trying response.send to forward the axios response to your client like so:

module.exports = (router) => {
    router.get('/profile', function(req, res) {
        //random fake profile info
        axios.get('https://randomuser.me/api/')
            .then(response => {
                console.log(response.data);
                // Send the axios response to the client...
                res.send(response.data)
            })
            .catch(error => {
                console.log(error);
            });           
    });
};
Related