【问题标题】:protobuf.js: print int64 Object as string in node.jsprotobuf.js:在 node.js 中将 int64 对象打印为字符串
【发布时间】:2016-05-01 20:18:33
【问题描述】:

我正在使用这个很棒的模块:protobuf.js 用于 node.js 中的协议缓冲区。

我有 int64 字段类型(由 google-protocol 支持),我正在尝试将其打印到屏幕上。

行:

console.log(MessageTypeAck.decode(message));

打印以下内容:

{ trace: 
   [ { topic: 'Genesis', partition: 0, offset: [Object] },
     { topic: 'DataManager', partition: 0, offset: [Object] } ],
  sourceModuleName: 'DataManager',
  sourceModuleID: 10040 }

(偏移量字段打印为[Object]) 和行:

console.log(MessageTypeAck.decode(message).trace[1].offset.toString());

打印:

9217

(实际值)。 为什么第一次调用没有调用 Long 对象的 toString()?

为什么期望的输出是:

{ trace: 
   [ { topic: 'Genesis', partition: 0, offset: 9217 },
     { topic: 'DataManager', partition: 0, offset: 9217 } ],
  sourceModuleName: 'DataManager',
  sourceModuleID: 10040 }

我找到了这个issue,但据我所知,我不应该做一些特别的事情来得到它..

我还找到了this - 我的版本(npm list protobufjs)返回protobufjs@5.0.1

有什么想法吗?

【问题讨论】:

  • 我也试过 .decode64 但还是不行..

标签: javascript node.js protocol-buffers proto


【解决方案1】:

在 Node.js 中 console.log calls util.inspect under the hood。默认是has a depth value of 2

为了打印一个对象的所有属性,你可以直接调用util.inspect

const util = require('util');

console.log(util.inspect(
    MessageTypeAck.decode(message),
    { showHidden: true, depth: null }
));

【讨论】:

  • console.log('%j', MessageTypeAck.decode(message)) 将对象输出为JSON。
猜你喜欢
  • 1970-01-01
  • 2021-06-11
  • 2016-03-31
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-09
  • 1970-01-01
相关资源
最近更新 更多