pavithra rox
pavithra rox

Reputation: 1086

Protobuf encode returns null values

I am trying to encode a message to a Buffer using the encode method in protobufjs.

this is my code.

setValue(value) {
        var address = makeAddress(value);
        let data = protobuf["Icao"].create({data:value})
        let container = protobuf["IcaoContainer"].create()
        container.entries = data
        console.log(container)
        var stateEntriesSend = {}
        stateEntriesSend[address] = protobuf['IcaoContainer'].encode(container).finish();
        console.log(stateEntriesSend[address])
        return  this.context.setState(stateEntriesSend, this.timeout).then(function(result) {
            console.log("Success", result)
          }).catch(function(error) {
            console.error("Error", error)
          })
      }

the value of console.log(container) is below, which is correct.

IcaoContainer {
  entries: 
   Icao {
     data: <Buffer a2 66 61 63 74 69 6f 6e 63 73 65 74 64 64 61 74 61 68 61 73 61 70 6f 69 75 79> } }

but I am trying to encode it as a buffer using protobuf['IcaoContainer'].encode(container).finish()

and it seems to return an empty buffer. value of console.log(stateEntriesSend[address]) is below

<Buffer >

my proto file.

syntax = "proto3";

message Icao {
  string data = 1;
}

message IcaoContainer {
  repeated Icao entries = 1;
}

What's wrong here?

Upvotes: 0

Views: 1422

Answers (2)

harkinian
harkinian

Reputation: 131

I was seeing empty buffers even though I verified first. Turns out protobuf.js ignores fields in plain JS objects that aren't present in the proto message, and still considers it valid. I had only one field in my message and misspelled it in the JS object, so it was outputting an empty message.

Upvotes: 0

pavithra rox
pavithra rox

Reputation: 1086

Just in case if someone else is getting the same problem I have figured out where this went wrong.

according to my Icao message, I have defined in my proto the data should be a string. But when I call the setValue method I am passing a JSON object instead of that string value. so when I use

protobuf["Icao"].create({data:value})

the value is not a string. that was the problem. according to protobufjs documentation, it is always a good practice to Verify the payload before you use create method it. like this.

 // Exemplary payload
    var payload = { awesomeField: "AwesomeString" };

    // Verify the payload if necessary (i.e. when possibly incomplete or invalid)
    var errMsg = AwesomeMessage.verify(payload);
    if (errMsg)
        throw Error(errMsg);

    // Create a new message
    var message = AwesomeMessage.create(payload); // or use .fromObject if conversion is necessary

    // Encode a message to an Uint8Array (browser) or Buffer (node)
    var buffer = AwesomeMessage.encode(message).finish();
    // ... do something with buffer

this threw me an error saying data must be String. that was the issue.

And also since I have used repeated keyword in my IcaoContainer container.entries should be an array.

Upvotes: 1

Related Questions