在下面的代码(在节点JS运行),我尝试打印使用JSON.stringify
这导致错误外部API获得的对象:如何打印对象节点JS
TypeError: Converting circular structure to 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}`);
});
http://stackoverflow.com/ question/4816099/chrome-sendrequest-error-typeerror-conversion-circular-structure-to-json – Cristy
你使用body-parser吗? 'res.country'不应该是未定义的。 – Cristy
@Cristy我没有使用body-parser; res.country打印为未定义。不知道它为什么这样做? – kurrodu