首页 > 解决方案 > 如何将 Map 值设置为数组?

问题描述

我想提出一个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;

标签: javascriptreactjsprotocol-buffersgrpc-web

解决方案


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


推荐阅读