Search code examples
javascriptnode.jsrpcgrpc

NodeJS gRPC: "Method handler expected but not provided"


I plowed through the docs and haven't found a solution yet. The app is loosely based on the "sayHello"-example from their docs but every time the code runs the warning Method handler for /eventComm.DatabaseRPC/InsertSingleDocument expected but not provided is returned.

My proto file:

service DatabaseRPC {
  rpc InsertSingleDocument (Doc) returns (Doc) {} 
}

message Doc {
  required string name = 1;
  required int32 id = 2;
}

My gRPC Server:

  function InsertSingleDocument (call, callback) {
    callback(null, {
      name: 'Hello ',
      id: 1
    })
  }
  let server = new grpc.Server()
  server.addProtoService(protoDef.DatabaseRPC.service, {
    InsertSingleDocument: InsertSingleDocument
  })
  server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure())
  server.start()

What is the problem with this code? Of course I already tried to google the error but found no solution


Solution

  • To conform with JavaScript naming conventions, methods should be provided with the first letter lowercased:

    server.addProtoService(protoDef.DatabaseRPC.service, {
      insertSingleDocument: InsertSingleDocument
    })
    

    You can see this in the Hello World example you linked. The method is declared as SayHello in the proto file, but is passed to the server as sayHello.

    Note: I agree that this is confusing, and I will try to improve the situation.