简体   繁体   English

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

[英]Why is my console log response from a POST request different to the one I see in my browser console?

I have created some auth service in my react app.我在我的 React 应用程序中创建了一些身份验证服务。

It executes in the react App.js constructor like this:它在 react App.js 构造函数中执行,如下所示:

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

The service is this one服务就是这个

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


        });
}

The problem is that the console.log(response) is logging an unexpected result, it doesnt result the same as my chrome console in the /network/verify(my POST request) tab问题是 console.log(response) 记录了一个意外的结果,它与 /network/verify(my POST request) 选项卡中的 Chrome 控制台的结果不同

Shouldn't it return the same?它不应该返回相同的吗?

Basically you need to either return the promise or use async await基本上,您需要返回承诺或使用 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;
            }


        });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在浏览器控制台的 response.config.data 中看到登录信息是否正常? - Is it normal that I can see login info in response.config.data in my browser console? 我在浏览器的存储选项卡中看到 cookies。 但是当我控制台记录它们时,我总是看到未定义 - I see cookies in storage tab in my browser. But when I console log them I always see undefined 我怎么看这个响应中的错误 return res.status(400).json({ errors: errors.array() }); 我只是在控制台中看到 400 错误请求 - How am I meant to see the errors in this response return res.status(400).json({ errors: errors.array() }); I just see a 400 bad request in my console 我的 api 发布请求在 postman 工具中工作,但它在浏览器的控制台中显示错误? - My api post request is working in postman tool but it is showing error in the console of a browser? 我的代码使用 console.log 离线运行,但不在浏览器中 - My code runs offline with console.log but not in browser 您在 POST 请求 NodeJS 快速服务器中的哪里看到 console.log 语句? - Where do you see console.log statements inside a POST request NodeJS express server? 如何在控制台上查看我上传的图片名称? - How I can see my uploading images' names on console? 为什么控制台不记录 [Object] 而不是我的数据? - Why doesn't the console log [Object] instead of my data? 为什么我的应用程序未到达console.log? - why isn't my application reaching the console.log? 为什么我的 res.status 在我控制台记录时什么都不显示? - why is my res.status showing nothing when I console log it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM