admin管理员组文章数量:1279014
I'm trying to print out the value of Fetch promise, but it shows undefined
when i console.log it.
Php server responds to this fetch with a jwt token in the body of http when I try it in Postman.
fetch("http://localhost:8001/api/login", {
method: 'post',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: 'username=' + this.state.username + '&password=' + this.state.password
})
.then(function (responseObject) {
console.log('Request succeeded with Response object', responseObject);
})
.then(function (result){
console.log(result);
})
.catch(function (error) {
console.log('Request failed', error);
});
}else{
console.log('You must enter username, password.');
}
I'm trying to print out the value of Fetch promise, but it shows undefined
when i console.log it.
Php server responds to this fetch with a jwt token in the body of http when I try it in Postman.
fetch("http://localhost:8001/api/login", {
method: 'post',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: 'username=' + this.state.username + '&password=' + this.state.password
})
.then(function (responseObject) {
console.log('Request succeeded with Response object', responseObject);
})
.then(function (result){
console.log(result);
})
.catch(function (error) {
console.log('Request failed', error);
});
}else{
console.log('You must enter username, password.');
}
Share
Improve this question
asked Jan 8, 2018 at 21:18
sandersander
1,6545 gold badges25 silver badges56 bronze badges
2
-
your first .then has no return statement, so returns
undefined
... which is why result in the second .then is undefined. try returnresponseObject.text()
orresponseObject.json()
in your first .then – Jaromanda X Commented Jan 8, 2018 at 21:37 -
Solved the problem by addding
return responseObject
to the first. then clause and `console.log(responseObject.text()); to the second .then clause, now my jwt token prints out. Thank you @Jaro – sander Commented Jan 10, 2018 at 14:52
3 Answers
Reset to default 8You have to use a formatter to log or do something with the data, following is a way of doing it
fetch('http://localhost:8001/api/login').then(
function(response) {
if (response.status !== 200) {
console.log('Problem in fetching');
return;
}
response.text().then(function(data) {
console.log(data);
});
})
You will have to call something like json()
or text()
off of responseObject
and return that result to get it into result
.
fetch("http://localhost:8001/api/login", {
method: 'post',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: 'username=' + this.state.username + '&password=' + this.state.password
})
.then(function (responseObject) {
console.log('Request succeeded with Response object', responseObject);
return responseObject;
})
.then(function (result){
console.log(result.text());
})
.catch(function (error) {
console.log('Request failed', error);
});
}else{
console.log('You must enter username, password.');
}
Solved the problem by addding return responseObject to the first .then clause and console.log(responseObject.text()); to the second .then clause, now my jwt token prints out.
本文标签: javascriptHow do I print Fetch promise value in ReactJsStack Overflow
版权声明:本文标题:javascript - How do I print Fetch promise value in ReactJs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741287096a2370335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论