将值分配给重复字段Protobufs打字条

发布于 2025-02-03 01:06:10 字数 1249 浏览 3 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

執念 2025-02-10 01:06:10

我发现了解决方案。我们需要使用dataupdate本身的设置器。因此,之类的事情会起作用。

function exec() {
  const servicePb = require('service_pb');
  const req = new servicePb.DataUpdateRequest();
  req.setFilepath('C:/Users/santhosh/test.txt');

  // here is the change we need to make
  const dataUpdate = new servicePb.DataUpdate();
  dataUpdate.setSource(1);
  dataUpdate.setDestination(2);
  req.setUpdatesList([dataUpdate]); // or req.addUpdates(dataUpdate) will also work

  // sending req.serializeBinary() as data to endpoint
}

I figured out the solution. We need to use the setter of DataUpdate itself. So something like following would work.

function exec() {
  const servicePb = require('service_pb');
  const req = new servicePb.DataUpdateRequest();
  req.setFilepath('C:/Users/santhosh/test.txt');

  // here is the change we need to make
  const dataUpdate = new servicePb.DataUpdate();
  dataUpdate.setSource(1);
  dataUpdate.setDestination(2);
  req.setUpdatesList([dataUpdate]); // or req.addUpdates(dataUpdate) will also work

  // sending req.serializeBinary() as data to endpoint
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文