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'
.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With