admin管理员组文章数量:1326304
This works
fetch('')
.then(response => response.json())
.then(json => console.log('4. userId = ' + json.userId))
to give me:
4. userId = 1
But when I try to use await
{
async function doFetch() {
const result = await fetch('');
console.log('8. await: ' + String(result.json()));
} doFetch();
}
I get the promise instead as output
8. await: [object Promise]
How to get at the JSON value?
Update
Thanks for answers:
console.log('8. await: ' + String(await result.json()));
I should have been clearer though, how to get the userId? I tried
console.log('8. await: ' + String(await result.json().userId));
but I get undefined
This works
fetch('https://jsonplaceholder.typicode./todos/1')
.then(response => response.json())
.then(json => console.log('4. userId = ' + json.userId))
to give me:
4. userId = 1
But when I try to use await
{
async function doFetch() {
const result = await fetch('https://jsonplaceholder.typicode./todos/1');
console.log('8. await: ' + String(result.json()));
} doFetch();
}
I get the promise instead as output
8. await: [object Promise]
How to get at the JSON value?
Update
Thanks for answers:
console.log('8. await: ' + String(await result.json()));
I should have been clearer though, how to get the userId? I tried
console.log('8. await: ' + String(await result.json().userId));
but I get undefined
Share Improve this question edited Jul 26, 2020 at 17:20 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Jul 9, 2020 at 16:14 Michael DurrantMichael Durrant 96.6k101 gold badges347 silver badges531 bronze badges 1- fixed typo of user=1 hardcoded to be userid (what the code actually has) – Michael Durrant Commented Jul 9, 2020 at 17:12
2 Answers
Reset to default 5When you need two then
calls in the first version, then expect to need two await
s in the second version. The json()
method returns a promise.
So change:
console.log('8. await: ' + String(result.json()));
To:
console.log('8. await: ' + String(await result.json()));
To get a property, just do as usual, but make sure you have awaited the object first. So first close the parentheses:
(async function () {
const result = await fetch('https://jsonplaceholder.typicode./todos/1');
console.log('8. await: ' + (await result.json()).userId);
})();
If you need more than just that one property, then just first get the whole object in a variable, and use that variable from then on:
(async function () {
const result = await fetch('https://jsonplaceholder.typicode./todos/1');
const obj = await result.json();
console.log('userId = ' + obj.userId);
})();
result.json()
will return a Promise
that you need to await
.
Currently, you are converting Promise
object returned by result.json()
into string and that is what gets logged on the console.
const result = await fetch('https://jsonplaceholder.typicode./todos/1');
const data = await result.json();
console.log('8. await: ' + data);
Edit:
how to get the userId
You are getting undefined
because you are converting object returned by await result.json()
in to string and when any object is converted to string, you get something like "[object Object]"
.
Just do not convert the object returned by await result.json()
into string and simple access the userId
property
console.log('8. await: ' + (await result.json()).userId);
See the following snippet
async function fetchData() {
const result = await fetch('https://jsonplaceholder.typicode./todos/1');
const data = await result.json();
console.log(String(data)); // what you are doing
console.log(data); // what you should do
}
fetchData();
本文标签: javascriptHow to get at my json value using await instead of just thenStack Overflow
版权声明:本文标题:javascript - How to get at my json value using await instead of just .then - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742207518a2433094.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论