0

我有以下 protobuf 定义:

service MyService {
    rpc ServiceMethod (ServiceMethodRequest) returns (ServiceMethodResponse) {}
}

message ServiceMethodRequest{
    string requestParam = 1;
}
message ServiceMethodResponse{
    Error error = 1;
    SomeObjectList data = 2;
}
message Error{
    string code = 1;
    string errorMessage = 2;
}

message SomeObject {
    string myobject = 1;
}
message SomeObjectList {
    repeated SomeObject myobjects = 1;
}

如您所见,我想返回格式化响应,因此我的 API 有一些标准化的响应方式。我像这样格式化了我的回复(由于可读性,这是 JSON 格式):

{"error":{"code":"-1","errorMessage":""},"data":{"myobjects":[{"myobject":"some string"},{"myobject":"another string"}]}}

但在客户端,我不断得到:

{ Error: 2 UNKNOWN: Unknown Error
    at Object.exports.createStatusError (.../node_modules/grpc/src/common.js:91:15)
    at Object.onReceiveStatus (.../node_modules/grpc/src/client_interceptors.js:1204:28)
    at InterceptingListener._callNext (.../node_modules/grpc/src/client_interceptors.js:568:42)
    at InterceptingListener.onReceiveStatus (.../node_modules/grpc/src/client_interceptors.js:618:8)
    at callback (.../node_modules/grpc/src/client_interceptors.js:845:24)
  code: 2,
  metadata: Metadata { _internal_repr: {}, flags: 0 },
  details: 'Unknown Error' }

当我尝试 console.log 响应对象时。

我究竟做错了什么?另外,是否有任何关于 grpc 的好书以及一些示例(C、C#、Java、Python、JavaScript)?提前致谢!

4

1 回答 1

-1

服务器.js

callback(null, message);
于 2020-10-11T13:16:58.977 回答