【问题标题】:Nodejs Date object to protobufNodejs 日期对象到 protobuf
【发布时间】:2018-10-19 14:46:53
【问题描述】:

我正在构建一个与 protobuf 集成的 graphql。我有一个 gql 架构,例如:

type User {
  userId: ID!,
  createTime: DateTime 
}

问题是如何仅通过构造函数创建User 消息对象?指定,我想做: message = new User(['my_user_id', '2018-10-01']).toObject() 以便它将转换为正确的 protobuf 消息对象给我。喜欢:

{    
  "userId": "my_user_id",
  "createTime": {
    "seconds": 15000000,
    "nanos": 0
  }
}

但是,createTime 始终是undefined

试了很多方法,还是不行(顺便说一下nodejs protobuf时间戳的网上资源少之又少)

我试过message = new User(['my_user_id', '2018-10-01T20:23:32.000Z']).toObject() 不工作

我试过message = new User(['my_user_id', new Date()]).toObject(),没用

我试过了

let timestamp = new proto.google.protobuf.Timestamp()
timestamp.setSeconds(1)
timestamp.setNanos(1)
message = new User(['my_user_id',timestamp]).toObject()

也不行

有谁知道正确的方法来做到这一点?非常感谢

【问题讨论】:

    标签: node.js protobuf.js


    【解决方案1】:

    我明白了。只需执行new User(['my_user_id', [1538425412, 0]]).toObject() 就会给出正确的消息对象

    {
      "userId": "my_user_id",
      "createTime": {
        "seconds": 1538425412,
        "nanos": 0
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-25
      • 2013-12-03
      • 2011-03-21
      • 2017-04-25
      • 1970-01-01
      • 2014-05-31
      • 2011-02-17
      • 1970-01-01
      相关资源
      最近更新 更多