NestJS GRPC Error: call.sendMetadata is not a function

NestJS GRPC Error: call.sendMetadata is not a function

我正在尝试使用 NestJS 框架从服务器端 gprc 发送元数据。在 NestJS 官方指南 here 中:它显示了执行服务器端 grpc 元数据的示例:

@Controller()
export class HeroesService {
    @GrpcMethod()
    findOne(data: HeroById, metadata: Metadata, call: ServerUnaryCall<any>): Hero {
        const serverMetadata = new Metadata();
        const items = [
          { id: 1, name: 'John' },
          { id: 2, name: 'Doe' },
        ];
    
        serverMetadata.add('Set-Cookie', 'yummy_cookie=choco');
        call.sendMetadata(serverMetadata);
    
        return items.find(({ id }) => id === data.id);
    }
}

在我的代码上,我写了一段类似的代码:

@GrpcMethod('ClinicService', 'GetCustomerClinicNames')
async GetCustomerClinicNames_GRPC(data: CustomerClinicID, call:ServerUnaryCall<Any,Any>) {
    const metadata = new Metadata();
    const result = await this.clinicService.GetClinicName(Number(data.customer_id));
    console.log(result);
    metadata.add('context', "Jello")
    call.sendMetadata(metadata)
    return { response: result};
}

但是,它给我一个错误说:

[Nest] 53188  - 04/06/2022, 5:17:50 PM   ERROR [RpcExceptionsHandler] call.sendMetadata is not a function
TypeError: call.sendMetadata is not a function
    at ClinicController.GetCustomerClinicNames_GRPC (C:\Users\Vibrant\Desktop\core-samples\src\clinic\clinic.controller.ts:118:10)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at C:\Users\Vibrant\Desktop\core-samples\node_modules\@nestjs\microservices\context\rpc-proxy.js:11:32

但我认为.sendMetadata 确实是一个函数。我哪里错了?

对于sendMetadata函数,定义为:

export declare type ServerSurfaceCall = {
    cancelled: boolean;
    readonly metadata: Metadata;
    getPeer(): string;
    sendMetadata(responseMetadata: Metadata): void;
    getDeadline(): Deadline;
} & EventEmitter;

export declare type ServerUnaryCall<RequestType, ResponseType> = ServerSurfaceCall & {
    request: RequestType;
};

显然您需要在 async GetCustomerClinicNames_GRPC 中保留 metadata 参数。我试过有和没有,只有当我有它时它才有效。

例如,我试过:

@GrpcMethod('HeroesService', 'FindOne')
findOne(data: any, call: ServerUnaryCall<any, any>): any

那没用,然后我试了:

@GrpcMethod('HeroesService', 'FindOne')
findOne(data: any, _metadata: Metadata, call: ServerUnaryCall<any, any>): any

它的工作原理是:

@GrpcMethod('HeroesService', 'FindOne')
findOne(data: any, _metadata: Metadata, call: ServerUnaryCall<any, any>): any {
    const items = [
        { id: 1, name: 'John' },
        { id: 2, name: 'Doe' },
    ];
    
    const metadata = new Metadata();
    metadata.add('context', 'Jello');
    call.sendMetadata(metadata);

    return items.find(({ id }) => id === data.id);
}

我不确定为什么,因为我们使用的是命名参数。