Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

265
Visualizações
nestjs with grpc microservices not return empty arrays

I call service via grpc in nest js and when it return empty array the property not returned in object

  @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);
    }
  }

I logged the result before it returned to client service it was

 {
      "id": "457a508b-f19a-4c77-b8a2-bb4004b9277b",
      "name": "plan1",
      "key": "plan1",
      "price": 20,
      "yearlyDiscountRate": 0.2,
      "trialDays": 14,
      "isRecommended": true,
      "isLegacy": false,
      "features": []
    }

but when it return to client via grpc it was

 {
      "id": "457a508b-f19a-4c77-b8a2-bb4004b9277b",
      "name": "plan1",
      "key": "plan1",
      "price": 20,
      "yearlyDiscountRate": 0.2,
      "trialDays": 14,
      "isRecommended": true,
      "isLegacy": false
    }

the features key with empty array not found.

I tried some things like add loader with 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,
        },
      },
    },
  );

but it still not return property with empty array

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I found the answer it was from line

if (error) {
        throw new BadRequestException(error);
      }

when arrays: true the if condition be true and throw an error. solved with

if (error?.length) {
        throw new BadRequestException(error);
      }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda