2

I'm learning how to build a micro services architecture by nestjs and gRPC I create an API to get data from another service, and it doesn't need any argument.

So I use google.protobuf.Empty and let validate interface function keep empty as code below.

contractAddress.proto

import "google/protobuf/empty.proto";
syntax = "proto3";

package contractAddress;

service ContractAddressService {
  rpc getAllContractAddress (google.protobuf.Empty) returns (GetAllContractAddressResponse);
}

message GetAllContractAddressResponse {
  repeated string data = 1;
}

grpc.interface.ts

export interface ContractServiceClient {
  getAllContractAddress(): Observable<GetAllContractAddressResponse>;
}

contract.controller.ts

  @Inject('CONTRACTADDRESS_PACKAGE')
  private client: ClientGrpc;

  onModuleInit(): void {
    this.svc = this.client.getService<ContractServiceClient>(
      'ContractAddressService',
    );
  }

  @Get()
  getContractAddres() {
    return this.svc.getAllContractAddress();
  }

But I encountered the error when I called this api. Does it mean I need to add argument absolutely? I have tried that adding argument, and it works well.

[Nest] 77656  - 09/12/2022, 5:54:19 PM   ERROR [ExceptionsHandler] Incorrect arguments passed
Error: Incorrect arguments passed
    at ServiceClientImpl.checkOptionalUnaryResponseArguments (/Users/champerwu/Documents/Project/naos-fin-interview/naos-api/node_modules/@grpc/grpc-js/src/client.ts:224:15)
    at ServiceClientImpl.makeUnaryRequest (/Users/champerwu/Documents/Project/naos-fin-interview/naos-api/node_modules/@grpc/grpc-js/src/client.ts:271:35)
    at ServiceClientImpl.getAllContractAddress (/Users/champerwu/Documents/Project/naos-fin-interview/naos-api/node_modules/@grpc/grpc-js/src/make-client.ts:189:15)
    at Observable._subscribe (/Users/champerwu/Documents/Project/naos-fin-interview/naos-api/node_modules/@nestjs/microservices/client/client-grpc.js:177:35)
    at Observable._trySubscribe (/Users/champerwu/Documents/Project/naos-fin-interview/naos-api/node_modules/rxjs/src/internal/Observable.ts:245:19)
    at /Users/champerwu/Documents/Project/naos-fin-interview/naos-api/node_modules/rxjs/src/internal/Observable.ts:235:18
    at Object.errorContext (/Users/champerwu/Documents/Project/naos-fin-interview/naos-api/node_modules/rxjs/src/internal/util/errorContext.ts:29:5)
    at Observable.subscribe (/Users/champerwu/Documents/Project/naos-fin-interview/naos-api/node_modules/rxjs/src/internal/Observable.ts:221:5)
    at /Users/champerwu/Documents/Project/naos-fin-interview/naos-api/node_modules/rxjs/src/internal/lastValueFrom.ts:59:12
    at new Promise (<anonymous>)
Champer Wu
  • 1,101
  • 3
  • 13
  • 32
  • 1
    I tried `this.svc.getAllContractAddress({});` `getAllContractAddress({}): Observable;` and it works successfully – Champer Wu Sep 12 '22 at 11:26

1 Answers1

3

Yes, you need to provide a message argument in all cases. google.protobuf.Empty is a predefined message type with no fields, but it is not otherwise special, and using it does not trigger any special code generation. You still need to provide an empty message object.

murgatroid99
  • 19,007
  • 10
  • 60
  • 95