protocolbuffers / protobuf-javascript

BSD 3-Clause "New" or "Revised" License
351 stars 67 forks source link

ES3 protobuf - getRepeatedWrapperField & wrapRepeatedField_ #24

Open prk68 opened 4 years ago

prk68 commented 4 years ago

I am trying to run protbuf serialization for JScript app that runs in a window scripting host runtime (WSH) on window. The JScript seems to based on a very old version of Ecmascript.

I webpacked my app code and ran into following error

"length is null or not an object"

for this line

for (var d = jspb.Message.getRepeatedField(a, c), e = [], f = 0; f < d.length; f++)

This seeems to be happening inside wrapRepeatedField_ function from a getRepeatedWrapperField call.

The same webpacked application code runs perfectly fine on chrome browser. So I am suspecting an issue either with missing polyfills because of which the message was not initialized properly.

I would appreciate any hints on how I could make this work on this old JScript runtime.

prk68 commented 4 years ago

Further logging tells me that this happens because jspb.Message.getField returns undefined. Both the following conditions do not match if (b < a.pivot_)

if (a.extensionObject_)

jelleklaver commented 4 years ago

Hi @prk68,

I seem to have a similar issue, but it occurs to me running in both Chrome and Firefox. I've been using protobuf with grpc-web for a while now, and it suddenly occured for a particular definition. Both errors point to the same line as you mentioned. I use parcel to generate the client code.

// Chrome console
TypeError: Cannot read property 'length' of undefined
    at Function.jspb.Message.wrapRepeatedField_ (google-protobuf.js:432)
    at Function.jspb.Message.getRepeatedWrapperField (google-protobuf.js:432)
    at proto.domain.Question.getAnswersList (domain_pb.js:4651)
    ...
// Firefox console
TypeError: d is undefined
    wrapRepeatedField_ google-protobuf.js:432
    getRepeatedWrapperField google-protobuf.js:432
    getAnswersList domain_pb.js:4651
    ...

I tried to regenerate the javascript files using protoc 3.12.4 and 3.13.0 and grpc-web 1.2.0 and 1.2.1, but both give the same results. The strange thing is, that it seems that sometimes setting the repeated field as an empty array (.setAnswersList([])) helps, and sometimes it doesn't.

I'm pretty clueless in how to debug this further. Did you find a solution?

Elojah commented 3 years ago

Any news on this bug ? I have exactly the same as @jelleklaver, i can reproduce to give more informations if necessary. Proto definition:

message FooResp {
    repeated foo.Bar MyField = 1;
}

(with foo.Bar containing only native strings)

jelleklaver commented 3 years ago

Hi @Elojah, we came across this again this week and we just figured out what happened in our case. Our protobuf definitions rely on a shared domain.proto, but with grpc-web we cannot use this as a single library (https://github.com/grpc/grpc-web/issues/776). The issue was that we regenerated one library with grpc-web, but in-code we referenced this shared dependency from another library. So the repeated field didn't exist in the referenced code.

Therefore, it was an issue on our side. I think a more readable error message like field x doesn't exist would massively improve the debugging experience here. I don't know if this helps you in any way @Elojah, but there's no more I know.

Elojah commented 3 years ago

Same on my side, protobuf files were not generated for typescript grpc-web but only typescript (without those fields, actually just a generation option to change). Thanks for the follow up @jelleklaver , and sorry for the false bug report.

dibenede commented 2 years ago

This sounds like a versioning issue and we might have something we can upstream to help out in the future.