为什么从POST请求获得的控制台日志响应与在浏览器控制台中看到的响应不同?

时间:2019-12-29 19:08:16

标签: node.js post axios

我已经在我的React应用中创建了一些身份验证服务。

它在react App.js构造函数中执行,如下所示:

if(isAuthenticated()){
        console.log("its true");
    }else{
        console.log("its false");
    }

服务就是这个

import axios from 'axios'
import Cookies from 'js-cookie';


export default function isAuthenticated(){
    var accesstoken = Cookies.get('accesstoken');


    axios({ method: 'post', url: 'http://localhost:3003/verify', headers: { Authorization: `Bearer ${accesstoken}` } })
        .then(function (response) {

            //thats not he response im seeing in network/verify/response, its a different one which always returns the same stuff(wrong) while the one I see in my chrome console is the good one
            console.log(response)

            if(response.data === "OK"){
                return true;
            }else{
                return false;
            }


        });
}

问题是console.log(response)记录了意外结果,它的结果与/ network / verify(我的POST请求)标签中的chrome控制台不同

它不返回相同的内容吗?

1 个答案:

答案 0 :(得分:1)

基本上,您需要返回promise或使用async await

export default function isAuthenticated(){
    var accesstoken = Cookies.get('accesstoken');


    return axios({ method: 'post', url: 'http://localhost:3003/verify', headers: { Authorization: `Bearer ${accesstoken}` } })
        .then(function (response) {

            //thats not he response im seeing in network/verify/response, its a different one which always returns the same stuff(wrong) while the one I see in my chrome console is the good one
            console.log(response)

            if(response.data === "OK"){
                return true;
            }else{
                return false;
            }


        });
}