Chu*_*Kul 10 typescript axios nestjs
我是 nest.js 初学者,我正在尝试用我的代码实现 Axios,但出现此错误,我想修复它。
--> starting at object with constructor 'ClientRequest'
| property 'socket' -> object with constructor 'Socket'
--- property '_httpMessage' closes the circle +188941ms
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 (D:\CUSportcomplex-register\sso-reg\node_modules\express\lib\response.js:1123:12)
at ServerResponse.json (D:\CUSportcomplex-register\sso-reg\node_modules\express\lib\response.js:260:14)
at ExpressAdapter.reply (D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\platform-express\adapters\express-adapter.js:24:57)
at RouterResponseController.apply (D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-response-controller.js:13:36)
at D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-execution-context.js:173:48
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-execution-context.js:47:13
at async D:\CUSportcomplex-register\sso-reg\node_modules\@nestjs\core\router\router-proxy.js:9:17
Run Code Online (Sandbox Code Playgroud)
这是我的 app.service.ts
async validateSSO(appticket): Promise<SsoContent> {
let instance = axios.create({
baseURL: "http://localhost:8080/",
headers: {
'DeeAppId': config.DeeAppId,
'DeeAppSecret': config.DeeAppSecret,
'DeeTicket': appticket
}
});
instance.get("/serviceValidation")
.then(response => {
this.ssoContent = response;
})
.catch(error => {
return (error);
});
return this.ssoContent;
}
Run Code Online (Sandbox Code Playgroud)
这是我的 app.controller.ts
@Get('validation/:appticket')
async validateSSO(
@Param('appticket') appticket: string
//DeeAppTicket is sented via Front-end
): Promise<SsoContent> {
return this.registeringService.validateSSO(appticket);
}
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助 :)
小智 24
今天我遇到了这个问题,我设法解决它的一种方法是而不是返回:
this.ssoContent = response;
Run Code Online (Sandbox Code Playgroud)
我回来了:
this.ssoContent = response.data;
Run Code Online (Sandbox Code Playgroud)
首先,nest.js 为您提供开箱即用的 HttpService,您可以通过 DI 注入它:https ://docs.nestjs.com/techniques/http-module
其次 - 您正在尝试存储整个响应对象,该对象是复杂的数据结构并包含错误消息中所述的循环依赖项(TypeError:将循环结构转换为 JSON)
您应该做的是映射您需要的数据而不是存储整个圆形对象,
或者您应该查找一些可以解析循环 json 的库:https : //www.npmjs.com/package/flatted
小智 5
“当您忘记将关键字await 添加到异步函数中时,也会发生这种情况。”
为我解决了。
我感觉多么愚蠢......
小智 5
第2023章
简短回答:
const response = await axios(url, body, options );
return response.data;
Run Code Online (Sandbox Code Playgroud)
长答案:
你还记得http请求对象吗?当您进行 axios 调用时,存储响应并返回相同的响应。您基本上返回整个 http 对象,这会导致循环依赖。因此,您只从中提取数据对象并返回。
| 归档时间: |
|
| 查看次数: |
20348 次 |
| 最近记录: |