【发布时间】:2016-12-10 15:35:00
【问题描述】:
在下面的代码中(在 Node JS 上运行),我尝试使用 JSON.stringify 打印从外部 API 获得的对象,这会导致错误:
TypeError:将循环结构转换为 JSON
我已经查看了有关此主题的问题,但没有任何帮助。有人可以建议:
a) 我如何从res 对象中获取country 值?
b) 我如何打印整个对象本身?
http.get('http://ip-api.com/json', (res) => {
console.log(`Got response: ${res.statusCode}`);
console.log(res.country) // *** Results in Undefined
console.log(JSON.stringify(res)); // *** Resulting in a TypeError: Converting circular structure to JSON
res.resume();
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
【问题讨论】:
-
你使用body-parser吗?
res.country不应未定义。 -
@Cristy 我没有使用 body-parser; res.country 打印为未定义。不知道为什么会这样?
标签: javascript json node.js