类型错误:将圆形结构转换为 JSON -->从构造函数“ClientRequest"的对象开始 [英] TypeError: Converting circular structure to JSON --> starting at object with constructor 'ClientRequest'
问题描述
我是 nest.js 的初学者,我正在尝试用我的代码实现 Axios,但发生了这个错误,我想修复它.
I am a nest.js beginner and I am trying to implement Axios with my code and this error occurs and I would like to fix it.
--> 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-registersso-reg
ode_modulesexpresslib
esponse.js:1123:12)
at ServerResponse.json (D:CUSportcomplex-registersso-reg
ode_modulesexpresslib
esponse.js:260:14)
at ExpressAdapter.reply (D:CUSportcomplex-registersso-reg
ode_modules@nestjsplatform-expressadaptersexpress-adapter.js:24:57)
at RouterResponseController.apply (D:CUSportcomplex-registersso-reg
ode_modules@nestjscore
outer
outer-response-controller.js:13:36)
at D:CUSportcomplex-registersso-reg
ode_modules@nestjscore
outer
outer-execution-context.js:173:48
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async D:CUSportcomplex-registersso-reg
ode_modules@nestjscore
outer
outer-execution-context.js:47:13
at async D:CUSportcomplex-registersso-reg
ode_modules@nestjscore
outer
outer-proxy.js:9:17
这是我的 app.service.ts
This is my 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;
}
这是我的 app.controller.ts
and this is my 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);
}
谢谢你的帮助:)
推荐答案
首先,nest.js 为您提供开箱即用的 HttpService,您可以通过 DI 注入它:https://docs.nestjs.com/techniques/http-module
First of all nest.js provides you HttpService out of the box that you may inject it through DI: https://docs.nestjs.com/techniques/http-module
第二 - 您正在尝试存储整个响应对象,该对象是复杂的数据结构并包含错误消息中所述的循环依赖项(TypeError:将循环结构转换为 JSON)
Second - you are trying to store whole response object which is complex data structure and contains circular dependencies as it stated in error message (TypeError: Converting circular structure to JSON)
你应该做的是要么映射你需要的数据,而不是存储整个圆形对象,
What you should do is either you map the data you need instead of storing whole circular object,
或者您应该查找一些可以解析循环 json 的库:https://www.npmjs.com/package/flatted
or you should look up to some libs that could parse circular json: https://www.npmjs.com/package/flatted
这篇关于类型错误:将圆形结构转换为 JSON -->从构造函数“ClientRequest"的对象开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!