grpc-web icon indicating copy to clipboard operation
grpc-web copied to clipboard

c[e].toArray is not a function

Open Silvia813 opened this issue 6 years ago • 2 comments

here is my proto

message AppMeta {
  string name = 1;
  int64 max_user = 2;
  repeated string cidrs = 3;
}

message IssueLicenseRequest {
  LicenseType type = 1;
  string organization = 2;
  string email = 3;
  int64 expire_at = 4;
  repeated AppMeta apps = 5;
}    

and here is my client code

export default async function createLicense (param) {
  const { apps, email, organization, expireAt } = param
  const request = new licenseManagerProto.IssueLicenseRequest()
  request.setType(1)
  request.setEmail(email)
  request.setOrganization(organization)
  request.setExpireAt(expireAt)
  request.setAppsList([{ name: 'workspace', maxUser: 10, cidrs: '192.168.1.0/24' }])    // get error
  return new Promise((resolve, reject) => {
    client().issueLicense(request, headers(), (err, res) => {
      if (err) {
        reject(err)
      } else {
        resolve(res.toObject())
      }
    })
  })
}

then I got the error

app.js:261470 Uncaught (in promise) TypeError: c[e].toArray is not a function
    at Function../node_modules/google-protobuf/google-protobuf.js.jspb.Message.setRepeatedWrapperField (app.js:261470)
    at proto.tes.license.IssueLicenseRequest.setAppsList (app.js:493772)
    at _callee$ (app.js:487742)

Silvia813 avatar Feb 26 '20 04:02 Silvia813

Plain JS objects don't get automatically converted to protos.

You also need to create an AppMeta object, the same way you are creating an IssueLicenseRequest object:

const appMeta = new licenseManagerProto. AppMeta()
appMeta.setName('workspace');
appMeta.setMaxUser(10);
...etc

and then pass that to request's appsList:

request.setAppsList([appMeta])

iCovarrubias avatar Apr 08 '20 18:04 iCovarrubias

Yes the protobuf "repeated" field API is a bit quirky - you have to new all the AppMeta object first. Put them in an array [appmeta1, appmeta2, appmeta3], then call .setAppsList(arr) on your IssueLicenseRequest object.

stanley-cheung avatar May 07 '20 19:05 stanley-cheung