Nestjs 为什么它会给出JSON.stringfy错误,即使我没有使用它?
我正在nodejs中构建应用程序,在那里我必须通过点击HTTPS端点来显示数据。我正在使用Swagger UI显示数据。我得到以下错误Nestjs 为什么它会给出JSON.stringfy错误,即使我没有使用它?,nestjs,stringify,Nestjs,Stringify,我正在nodejs中构建应用程序,在那里我必须通过点击HTTPS端点来显示数据。我正在使用Swagger UI显示数据。我得到以下错误 Converting circular structure to JSON +1169ms TypeError: Converting circular structure to JSON at JSON.stringify (<anonymous>) at stringify (node_modules/express/lib/re
Converting circular structure to JSON +1169ms
TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at stringify (node_modules/express/lib/response.js:1123:12)
at ServerResponse.json (node_modules/express/lib/response.js:260:14)
at ExpressAdapter.reply (node_modules/@nestjs/platform-express/adapters/express-adapter.js:23:57)
at RouterResponseController.apply (node_modules/@nestjs/core/router/router-response-controller.js:10:36)
at @nestjs/core/router/router-execution-context.js:163:48
at process._tickCallback (internal/process/next_tick.js:68:7)
这是我的服务
import Axios, { AxiosResponse } from 'axios';
@Injectable()
export class MessageService {
constructor(private readonly httpService: HttpService) {}
configEndPoint: string =
'https:www.xyz.com';
getData(
source: string,
productCode: string,
vehicleType: string,
): Observable<any> {
return this.httpService.get(this.configEndPoint, { validateStatus: null });
}
}
导入Axios,{AxiosResponse}来自“Axios”;
@可注射()
导出类消息服务{
构造函数(私有只读httpService:httpService){}
configEndPoint:字符串=
“https:www.xyz.com”;
获取数据(
资料来源:string,
productCode:string,
车辆类型:字符串,
):可见{
返回this.httpService.get(this.configEndPoint,{validateStatus:null});
}
}
您不应该订阅可观察对象,NestJS将在引擎盖下处理该问题,只需将未订阅的可观察对象返回控制器并让Nest处理即可
即使您没有使用JSON.stringify
错误,也是因为express
在其send
方法中使用了它。AxiosResponse
类型(HttpService返回的内容)本身具有循环引用,因此您不需要发送完整的响应(返回整个响应是一种不好的做法,额外的数据太多)。您可以改为使用管道中的映射
操作符映射要发回的res的哪些部分。范例
@Injectable()
导出类消息服务{
构造函数(私有只读httpService:httpService){}
configEndPoint:字符串=
“https:www.xyz.com”;
获取数据(
资料来源:string,
productCode:string,
车辆类型:字符串,
):可见{
返回此.httpService.get(this.configEndPoint,{validateStatus:null}).pipe(
映射(res=>res.data)
);
}
}
这将获取AxiosResponse
的data
属性,并只允许将其发送回。好的,我如何以及在哪里可以指定需要从端点获取哪些数据?你是什么意思?您正在决定调用哪个端点。您应该看看您正在使用的API以及它的返回是什么样子的。data
属性就是所使用的端点返回的内容,它的外观取决于API是什么以及它是如何开发的。我明白你的意思,我想知道端点是否包含JSON数据数组,那么我应该在哪里修改代码以获取第一个数组数据?我们什么时候开始讨论数据数组的?返回的结构是一个JSON,data
属性可以是一个数组,具体取决于端点的返回,但这不能保证。如果您正在谈论接收多个响应,那么除非您进行多个调用,否则不应该发生这种情况
import Axios, { AxiosResponse } from 'axios';
@Injectable()
export class MessageService {
constructor(private readonly httpService: HttpService) {}
configEndPoint: string =
'https:www.xyz.com';
getData(
source: string,
productCode: string,
vehicleType: string,
): Observable<any> {
return this.httpService.get(this.configEndPoint, { validateStatus: null });
}
}