[英]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.