I have an axios get request like:
axios.get(url + '?hapikey=' + copyFrom)
.then(res => console.log(res.data));
which returns a response similar to:
{ name: 'Name',
anObject: [
{
id: 1,
nestedObject: [Object]
}
]
}
How can I get the response of nestedObject
to show its field/value pairs or is the only way to query it directly in the .then
?
I have an axios get request like:
axios.get(url + '?hapikey=' + copyFrom)
.then(res => console.log(res.data));
which returns a response similar to:
{ name: 'Name',
anObject: [
{
id: 1,
nestedObject: [Object]
}
]
}
How can I get the response of nestedObject
to show its field/value pairs or is the only way to query it directly in the .then
?
-
You just need to use
res.data.anObject[0].nestedObject
. However,anObject
is an array, which suggests there may be more than one returned by the api. – Mark Commented May 21, 2018 at 15:16 -
Yes there is this is just a simple example. So the only way would be to query it inside by
get
? There's no way to get the response to automatically show all Objects expanded out – Vincent Nguyen Commented May 21, 2018 at 15:18 -
2
@VincentNguyen if you only want to print the full object you should take a look into
JSON.stringify
– J. Pichardo Commented May 21, 2018 at 15:22 -
1
Looking more closely,
anObject
is valid. Is it an array or an object? Arrays don't havekey: value
pairs. – Mark Commented May 21, 2018 at 15:23 -
1
You can use
console.dir(obj, { depth: null })
to have the console show the whole object. – Mark Commented May 21, 2018 at 15:27
1 Answer
Reset to default 7The easiest way to deep print an object is to use JSON.stringify
, something like:
axios.get(url + '?hapikey=' + copyFrom)
.then(res => console.log(JSON.stringify(res.data)));
And as @PatrickRoberts mented you could use the optional parameters to pretty print
axios.get(url + '?hapikey=' + copyFrom)
.then(res => console.log(JSON.stringify(res.data, null, 2)));
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744262168a4565688.html
评论列表(0条)