1

I'm using gRPC with NodeJs and the response I want is this (example in JS format):

[
  ['aa1', 0, 0],
  ['aa2', 0, 0],
  ...
  ['aaN', 0, 0],
]

Is a list inside another list. Like a 2-Dimensional array or a matrix.

The problem here is I haven't seen examples returning a list inside an object without key/value fields, so I don't know how to create the .proto file.

Now I have something like this:

message Response{
  repeated ResponseData data = 1;
}
message ResponseData {
  repeated string field1 = 1;
  repeated uint32 field2 = 2;
  repeated uint32 field3 = 3;
}

And into node file to get and read the data I have something like:

client.getData(request, {}, (err, response)=> {
  var result = response.getDataList();
  // ^^^^^^^ here I want to get the matrix
}

Also I've tried with:

var result = response.getDataList();
result.forEach(element => {
  console.log("element 1 = ",element.getField1List())
  console.log("element 2 = ",element.getField2List())
  console.log("element 3 = ",element.getField3List())
})

But arrays returned from element.getFieldXList() are empty.

Also from server I'm sending this test matrix:

[["0",0,0],["1",1,1],["2",2,2],["3",3,3]]

Any help is apreciated.
Thanks in advance.

Edit:
Using console.log(JSON.stringify(result)) the output is:

[{"wrappers_":null,"arrayIndexOffset_":-1,"array":[[],[],[]],"pivot_":1.7976931348623157e+308,"convertedPrimitiveFields_":{}},{"wrappers_":null,"arrayIndexOffset_":-1,"array":[[],[],[]],"pivot_":1.7976931348623157e+308,"convertedPrimitiveFields_":{}},{"wrappers_":null,"arrayIndexOffset_":-1,"array":[[],[],[]],"pivot_":1.7976931348623157e+308,"convertedPrimitiveFields_":{}},{"wrappers_":null,"arrayIndexOffset_":-1,"array":[[],[],[]],"pivot_":1.7976931348623157e+308,"convertedPrimitiveFields_":{}}]
J.F.
  • 13,927
  • 9
  • 27
  • 65
  • Can you `console.log(JSON.stringify(result))` inside the `getData` callback and show us the output? – eol Dec 29 '20 at 14:40
  • Yes! Updated question with the `console.log()` result. – J.F. Dec 29 '20 at 14:46
  • Oh ok, so this is definitely an issue on the grpc server side as the elements in each `array` are empty. I thought you had problems mapping it in your nodejs code. As I have no experience with grpc I can't help here, sorry :) – eol Dec 29 '20 at 14:54
  • Oh ok, thanks for your advice anyway. I will take a look to the server but now it sends this object: `[["0",0,0],["1",1,1],["2",2,2],["3",3,3]]` so should be ok, so maybe the problem is with the `proto` structure and trying to map this list... – J.F. Dec 29 '20 at 14:59

1 Answers1

1

The protobuf message definition in the question describes an object that contains a list of objects, each of which has three fields, which each have a list of primitive values. However, you said that you tried to send a list of lists, which is a very different structure. To send a message, you need to provide an object with the same structure. For the message types provided, that would look something like this:

response = {
  data = [
    {
      field1: ["0"],
      field2: [0],
      field3: [0]
    },
    {
      field1: ["1"],
      field2: [1],
      field3: [1]
    },
    // ...
  ]
}

If you want to represent something more similar to the list of lists in the question, you probably want the inner fields of the ResponseData message to not be repeated.

murgatroid99
  • 19,007
  • 10
  • 60
  • 95
  • Thanks a lot for the answer, I will take a look. But I want a list of list. Is this a solution for an object list? I'm looking for a matriz solution, as the first example. – J.F. Jan 06 '21 at 21:04
  • You can't directly represent a list of lists in Protobuf. You need to translate it to some kind of message format like you have in the question. – murgatroid99 Jan 06 '21 at 21:05
  • I didn't know that. Then I will check your solution tomorrow. Thanks a lot! – J.F. Jan 06 '21 at 21:06