如何从快递服务器return json?
How to return json from express server?
我已经用 express.js 构建了一个服务器,它的一部分如下所示:
app.get("/api/stuff", (req, res) => {
axios.get('http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1').then(function(response){
res.send(response);
console.log('response=',response);
})
});
当我点击 'api/stuff' 时,它 return 出现错误:
(node:1626) UnhandledPromiseRejectionWarning: Unhandled promise
rejection (rejection id: 1): TypeError: Converting circular structure
to JSON
如何从我的端点 return json?
你从开放天气 API 得到的 response
对象是圆形类型(引用自身的对象)。 JSON.stringify
当它通过循环引用时会抛出错误。这就是您在使用 send
方法时出现此错误的原因。
为避免这种情况,只需发送所需的数据作为响应
app.get("/api/stuff", (req, res) => {
axios.get('http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1').then(function(response){
res.send(response.data);
console.log('response=',response.data);
})
});
我已经用 express.js 构建了一个服务器,它的一部分如下所示:
app.get("/api/stuff", (req, res) => {
axios.get('http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1').then(function(response){
res.send(response);
console.log('response=',response);
})
});
当我点击 'api/stuff' 时,它 return 出现错误:
(node:1626) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Converting circular structure to JSON
如何从我的端点 return json?
你从开放天气 API 得到的 response
对象是圆形类型(引用自身的对象)。 JSON.stringify
当它通过循环引用时会抛出错误。这就是您在使用 send
方法时出现此错误的原因。
为避免这种情况,只需发送所需的数据作为响应
app.get("/api/stuff", (req, res) => {
axios.get('http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1').then(function(response){
res.send(response.data);
console.log('response=',response.data);
})
});