0

I am trying to set-up the GRPC React.js client with Ruby server. I have tested the server with simple Ruby client and it worked so moved on and tried to implement it in my React app, but I get an error TypeError: response.getMessage is not a function when trying to get the message. I can see the requests correctly end up in the server and when I look at the response itself it looks like it holds the value I expect (Hello). What could be the problem?

# my_component.js
componentDidMount() {
    const request = new Point()
    request.setSensorId(1)
    request.setDays(7)

    this.client.getHistoricalData(request, {}, (err, response) => {
      if (err) {
        console.log(
          `Unexpected error for sayHello: code = ${err.code}` +
            `, message = "${err.message}"`
        )
      } else {
        console.log(response)
        console.log(response.getMessage())
      }
    })
  }
# response
{
    "wrappers_": null,
    "arrayIndexOffset_": -1,
    "array": [
        "Hello"
    ],
    "pivot_": 1.7976931348623157e+308,
    "convertedPrimitiveFields_": {}
}
nerwusik
  • 71
  • 1
  • 5

1 Answers1

0

Since the response variable is a JSON, you cannot call the method getMessage since the object does not have this method. To do so, you can access the values inside of the JSON with json.attribute.

For example, in your case, response.pivot_ should work and return 1.7976931348623157e+308 and to get the Hello message you can call console.log(response.array[0]).

As an alternative, you can create a method of getMessage, where you pass the response, and extract the message.

getMessage(json) {
  return json.array[0]
}

And, when calling,console.log(getMessage(response)), it should return Hello

Kypps
  • 326
  • 2
  • 5