Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Protocol Buffers: c.toArray is not a function when setting a date value on my request

I'm trying to build a create request for grpc-web. I've got my protocol buffers generated and I can successfully fetch information but I'm having trouble creating a request.

Eg.

const request = new PricingMethodRequest()
request.setCurrencyId(64)
request.setId(0)
request.setFrequency(1)
request.setFromDate({ nanos: 0, seconds: 1555064508 }) // <--- Crashes on this line
...

It seems like I keep getting TypeError: c.toArray is not a function when I try to set a date value. Or even a price value which is also an object.

How do I implement setting a date value, or any value that's expecting a JavaScript object?

Edit:

I've seen stuff online that I could do something like this:

const fromDateAny = new proto.google.protobuf.Any.fromJavaScript({ nanos: 0, seconds: 1555064508 })
request.setFromDate(fromDateAny)

But doing this gives me the error Cannot find name 'proto'.

like image 577
Barry Michael Doyle Avatar asked Apr 11 '19 10:04

Barry Michael Doyle


People also ask

How do you define a protobuf?

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.

Does Google use protobuf?

Protocol buffers, or Protobuf, is a binary format created by Google to serialize data between different services. Google made this protocol open source and now it provides support, out of the box, to the most common languages, like JavaScript, Java, C#, Ruby and others.

What is Google protobuf message?

namespace google::protobuf. Defines Message, the abstract interface implemented by non-lite protocol message objects. Although it's possible to implement this interface manually, most users will use the protocol compiler to generate implementations.


Video Answer


1 Answers

It turns out I had to create a Timestamp type for the fromDate.

I did that by doing this:

import * as timestamp_pb from 'google-protobuf/google/protobuf/timestamp_pb'

...

const timestampFromDate = new timestamp_pb.Timestamp()
timestampFromDate.setSeconds(fromdate.seconds)
timestampFromDate.setNanos(fromDate.nanos)

request.setFromDate(timestampFromDate)
like image 106
Barry Michael Doyle Avatar answered Jan 03 '23 06:01

Barry Michael Doyle