javascript - Axios return nested objects in response verbosely - Stack Overflow

I have an axios get request like:axios.get(url + '?hapikey=' + copyFrom).then(res => conso

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?

Share Improve this question edited May 21, 2018 at 15:23 Vincent Nguyen asked May 21, 2018 at 15:14 Vincent NguyenVincent Nguyen 1,6834 gold badges20 silver badges34 bronze badges 8
  • 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 have key: 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
 |  Show 3 more ments

1 Answer 1

Reset to default 7

The 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信