将值分配给重复字段Protobufs打字条
我在service.proto中有一个Protobuf消息
message DataUpdate {
uint32 source = 1;
uint32 destination = 2;
}
message DataUpdateRequest
{
string filepath = 1;
repeated DataUpdate updates = 2;
}
,我使用protoc
来获取上述Protobuf文件的JS文件,我分配了如下的typescript文件中的值,
function exec() {
const servicePb = require('service_pb');
const req = new servicePb.DataUpdateRequest();
req.setFilepath('C:/Users/santhosh/test.txt');
req.setUpdatesList([{
source: 1,
destination: 2,
}]); // <- this line is throwing an error
// sending req.serializeBinary() as data to endpoint
}
setupDatesList上的错误是:
TypeError: c[e].toArray is not a function
at Function.push.../../node_modules/google-protobuf/google-protobuf.js.jspb.Message.setRepeatedWrapperField (google-protobuf.js:511:1)
我应该如何分配 :对这类消息的价值?
我从Postman尝试使用以下JSON主体(以及Content-type:application/json)尝试,它
{
"filepath": "C:/Users/santhosh/test.txt",
"updates": [
{
"destination": 2,
"source": 1
}
]
}
可以如何为打字稿中的重复字段更新列表分配值?
I have a protobuf message in service.proto
message DataUpdate {
uint32 source = 1;
uint32 destination = 2;
}
message DataUpdateRequest
{
string filepath = 1;
repeated DataUpdate updates = 2;
}
and I used protoc
to get the js file for the above protobuf file and I assigned the values as follows in typescript file
function exec() {
const servicePb = require('service_pb');
const req = new servicePb.DataUpdateRequest();
req.setFilepath('C:/Users/santhosh/test.txt');
req.setUpdatesList([{
source: 1,
destination: 2,
}]); // <- this line is throwing an error
// sending req.serializeBinary() as data to endpoint
}
The error on setUpdatesList is:
TypeError: c[e].toArray is not a function
at Function.push.../../node_modules/google-protobuf/google-protobuf.js.jspb.Message.setRepeatedWrapperField (google-protobuf.js:511:1)
How should I assign value to this type of message?
I tried from postman with the following json body (and Content-Type: application/json) and it worked
{
"filepath": "C:/Users/santhosh/test.txt",
"updates": [
{
"destination": 2,
"source": 1
}
]
}
How should I assign value to repeated field updatesList in typescript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了解决方案。我们需要使用dataupdate本身的设置器。因此,之类的事情会起作用。
I figured out the solution. We need to use the setter of DataUpdate itself. So something like following would work.