【问题标题】:Why Axios response doesn't console log result?为什么 Axios 响应没有控制台日志结果?
【发布时间】:2021-07-06 12:24:50
【问题描述】:

我正在开发一个后端 API,但有时我需要从另一个 API 获取用户数据。为了做到这一点,我正在尝试使用 Axios 发出 http 请求。该请求按预期在浏览器中返回结果,但问题是我无法在终端中显示控制台日志。即使我要求程序这样做,它也没有显示任何内容。我的代码可能有问题吗?

这是我的代码:

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);
            });



       
    });
};

【问题讨论】:

    标签: javascript node.js axios


    【解决方案1】:

    我建议尝试response.send 将 axios 响应转发给您的客户端,如下所示:

    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);
                });           
        });
    };
    

    【讨论】:

    • @nothingelse 如果对你有帮助,记得接受答案。
    猜你喜欢
    • 2018-03-27
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    相关资源
    最近更新 更多