2

I want to send an array of objects into grpc call with reactjs using the "grpc-web-client" module. but it is giving me an error while sending an array of objects. Following is the error:

TypeError: n.toArray is not a function

I don't understand why it is not sending this. and following is my structure of an array of objects which I want to send:

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

and in .proto file it is defined as follow:

message EventProfile {
repeated Events events = 2;
}

message EventSlots {
string title = 1;
}

Please, someone, help me to solve this problem.

  • did you generate js stubs from proto file? – user2063635 Mar 20 '20 at 10:31
  • @user2063635 yes – Tecore Demo Mar 20 '20 at 11:12
  • Are you sure you're using the `grpc-web-client` npm module? That has been deprecated. But if you are using `protoc --js_out` to generate the JS classes for messages, the syntax should be something like eventProfile->setEventsList([event1, event2]); whereas event1 and event2 are object you instantiated from the Events class. – Stanley Cheung Mar 26 '20 at 03:17

1 Answers1

1

To send an array you have to construct a collection of proper objects. I assume that your proto looks something like that:

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

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

Then after generating code from proto sending can be realized like that:

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");
})