javascript - 如何将 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;
解决方案
我认为您不能将数组设置为值。定义看起来像地图以 SetOfString 作为值。您应该设置 SetOfString 的实例
推荐阅读
- jquery - 逐步滚动
- gradle - 运行 Gradle 应用程序时如何摆脱执行/进度条?
- javascript - 使用 base64 函数导致 InvalidCharacterError
- flutter - 如何在文本字段暂停后执行 onchanged?(Flutter)
- ruby-on-rails - Rspec:测试私有方法时出错
- node.js - Nodejs - 如何将函数应用于两个流的每一行组合?
- django - 在一组 Max'd 值上运行 Window 函数
- asp.net-mvc - 本地主机和生产环境的 Azure Active Directory 应用程序注册
- c - 如何编写“选择性”Makefile?
- installation - 在具有 Anaconda 的 Linux 机器上安装 Sage 会导致错误,但希望两者都有