【问题标题】:How do I set Map value to an Array?如何将 Map 值设置为数组?
【发布时间】:2019-09-02 19:23:12
【问题描述】:

我想提出一个 grpc 请求。请求在 proto 文件中有一个 map 字段。我已经使用 protoc 为 protos 生成了 js 文件。我无法在请求中定义 Map。

var req_rg = new ResourceGroup();
req_rg.setResourcegroupname(payload.resourceGroupName);
req_rg.setResourcegroupid(payload.resourceGroupId);
req_rg.setTenantlinkid(TENANT_ID);
req_rg.getResourceoperationmapMap().set('resource', ['op1', 'op2'])

这是消息的 .proto 文件。

message ResourceGroup{

    string resourceGroupName = 1;
    string resourceGroupDescription = 2;
    map<string,SetOfString> resourceOperationMap = 3;
    string tenantLinkId = 4;
    map<string,string> attributes = 5;
    string resourceGroupId = 6;

}

我收到此错误。

TypeError: b.toArray is not a function
push../node_modules/google-protobuf/google-protobuf.js.jspb.Map.set
node_modules/google-protobuf/google-protobuf.js:4666
  4663 | 
  4664 | jspb.Map.prototype.set = function (a, b) {
  4665 |   var c = new jspb.Map.Entry_(a);
> 4666 |   this.valueCtor_ ? (c.valueWrapper = b, c.value = b.toArray()) : c.value = b;
  4667 |   this.map_[a.toString()] = c;
  4668 |   this.arrClean = !1;
  4669 |   return this;

【问题讨论】:

    标签: javascript reactjs protocol-buffers grpc-web


    【解决方案1】:

    我认为您不能将数组设置为值。 该定义看起来像地图将 SetOfString 作为值。 您应该设置 SetOfString 的实例

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 2013-06-12
      • 2017-10-26
      • 1970-01-01
      • 2012-10-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多