将循环结构转换为JSON时出错...采取2

问题描述

如何解决错误?我在GoormIDE中工作。我早些时候发布了这个问题,但是不得不删除该帖子。我检查了其他帖子,但我仍然是初学者,并且正在按照教程进行操作,因此,希望您能就此特定问题提供帮助。我看到它与JSON.stringify有关...但是我不知道它是什么或它将去哪里...感谢您的帮助!

var express = require('express');
var app = express();
const axios = require('axios');

app.listen(3000,function() { 
  console.log('Server listening on port 3000'); 
});

app.get("/results",function(req,res){
    axios.get("http://www.omdbapi.com/?apikey=<myKey>&s=california")
    .then(function (response){
        res.send(response);
    })
    .catch(function (error){
        console.log("There is an Error");
        console.log(error);
    })
})

错误

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'ClientRequest'
    |     property 'socket' -> object with constructor 'Socket'
    --- property '_httpMessage' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (/workspace/webDevBootcamp2/APIs/API_Movie_App/node_modules/express/lib/response.js:1123:12)
    at ServerResponse.json (/workspace/webDevBootcamp2/APIs/API_Movie_App/node_modules/express/lib/response.js:260:14)
    at ServerResponse.send (/workspace/webDevBootcamp2/APIs/API_Movie_App/node_modules/express/lib/response.js:158:21)
    at /workspace/webDevBootcamp2/APIs/API_Movie_App/app.js:12:7
    at processticksAndRejections (internal/process/task_queues.js:97:5)

解决方法

axios中的响应对象不仅是来自请求的响应数据-这样,由于它是一个复杂的对象,因此将包含“圆形”引用

但是,响应数据自然会采用可以发送的格式

所以,您要做的就是发送响应数据

.then(function (response){
    res.send(response.data);
})