Llamo al servicio a través de grpc en nest js y cuando devuelve una matriz vacía, la propiedad no se devuelve en el objeto
@Get() @Public() async findAll( @Query() request: PaginationQuery, ) { try { const res$ = this.svc.getAll(request); const { error, ...rest } = await firstValueFrom(res$); if (error) { throw new BadRequestException(error); } return rest; } catch (e) { this.logger.error(e); return new InternalServerErrorException(e); } }
Registré el resultado antes de que volviera al servicio de atención al cliente.
{ "id": "457a508b-f19a-4c77-b8a2-bb4004b9277b", "name": "plan1", "key": "plan1", "price": 20, "yearlyDiscountRate": 0.2, "trialDays": 14, "isRecommended": true, "isLegacy": false, "features": [] }
pero cuando volvió al cliente a través de grpc fue
{ "id": "457a508b-f19a-4c77-b8a2-bb4004b9277b", "name": "plan1", "key": "plan1", "price": 20, "yearlyDiscountRate": 0.2, "trialDays": 14, "isRecommended": true, "isLegacy": false }
no se encontró la clave de features
con la matriz vacía.
Probé algunas cosas como agregar un cargador con arrays:true
const app = await NestFactory.createMicroservice<MicroserviceOptions>( SubscriptionsModule, { transport: Transport.GRPC, options: { url: '0.0.0.0:50052', package: protobufPackage, protoPath: join(process.cwd(), 'libs/proto/src/subscriptions.proto'), loader: { arrays: true, }, }, }, );
pero aún no devuelve la propiedad con una matriz vacía
Encontré la respuesta que era de la línea
if (error) { throw new BadRequestException(error); }
when arrays: true
la condición if es verdadera y genera un error. resuelto con
if (error?.length) { throw new BadRequestException(error); }