【发布时间】:2017-12-11 06:01:27
【问题描述】:
如this issue 中所述,我们无法使用 Node 实现来实现 gRPC 客户端,因为“RN 不是纯 Node”。
因此,我开始使用 Native Modules 进行 Objective-C 实现。
[service postWithRequest:request handler:^(RequestConfirmation * _Nullable response, NSError * _Nullable error) {
if (response) {
// This prints correctly in my JS console
RCTLogInfo(@"%@", response.message);
// This generates an error, see below
resolve(response);
// This works
NSDictionary *formattedResponse = @{
@"id": response.id_p,
@"message": response.message
};
resolve(formattedResponse);
} else {
reject(@"error", @"An error occurred while saving", error);
}
}];
错误:
RCTJSONStringify() encountered the following error: Invalid type in JSON write (RequestConfirmation)
如您所见,问题出在resolve 方法上。我想 React 没有找到任何方法将我的原始消息转换为 JSON。
如何保持响应不变并将其传递给 resolve 方法? 然后我可以在我的 JS 代码中对其进行解码。
谢谢。
编辑 1:
RequestConfirmation 在我的proto 文件中定义如下:
message RequestConfirmation {
string id = 1;
string message = 2;
}
然后在Objective-C中生成:
@interface RequestConfirmation : GPBMessage
@property(nonatomic, readwrite, copy, null_resettable) NSString *id_p;
@property(nonatomic, readwrite, copy, null_resettable) NSString *message;
@end
【问题讨论】:
标签: node.js react-native protocol-buffers grpc react-native-ios