2

我想使用“grpc-web-client”模块将一组对象发送到带有 reactjs 的 grpc 调用中。但它在发送对象数组时给了我一个错误。以下是错误:

TypeError: n.toArray is not a function

我不明白为什么它不发送这个。以下是我要发送的对象数组的结构:

let events = [{
  title:"event title"
}];

在 .proto 文件中定义如下:

message EventProfile {
repeated Events events = 2;
}

message EventSlots {
string title = 1;
}

请有人帮我解决这个问题。

4

1 回答 1

1

要发送一个数组,您必须构造一个适当对象的集合。我假设您的原型看起来像这样:

message EventProfile {
    repeated Event events = 1;
}
message Event {
    string title = 1;
}
message EventResponse {}

service EventService {
    rpc sendEvent(EventProfile) returns (EventResponse) {}
}

然后从proto发送生成代码后可以这样实现:

let myEvents = [{title:"Title A"}, {title:"Title B"}]; // example of data you want to send collected in the app  

let client = new EventServiceClient("http://localhost:8000", null, null); // example of client setup

let protoEventsList = [];
myEvents.forEach((event) => {
    let protoEvent = new Event();
    protoEvent.setTitle(event.title);
    protoEventsList.push(protoEvent);
})

let request = new EventProfile();
request.setEventsList(protoEventsList);

client.sendEvent(request, {}, () => {
    console.log("Done");
})
于 2020-06-06T11:12:29.637 回答