javascript - Thrift Node JavaScript 命名空间

标签 javascript node.js conflict thrift

我似乎遇到了 Node/thrift 命名空间冲突。

Foo.thrift
...
struct Error {
    1: i32 code,
    2: string message
}
...

通过 thrift --gen js:node Foo.thrift (thrift v0.9.0) 生成以下文件

Foo_types.js
...
Error = module.exports.Error = function(args) {
  this.code = null;
  this.message = null;
  if (args) {
    if (args.code !== undefined) {
      this.code = args.code;
    }
    if (args.message !== undefined) {
      this.message = args.message;
    }
  }
};
Error.prototype = {};
Error.prototype.read = function(input) {
...

我将模块包含在 Node 中

var FooTypes = require('./../gen-nodejs/Foo_types')

我似乎遇到了与 javascript 的 Error 对象的命名空间冲突

callback(new Error("Couldn't find profile"));

在回调中,它显示我有一个带有 codemessage 的对象,而不是一个包含“message”的普通旧 JS 错误,即使我没有要求FooTypes.Error

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Error

还有其他人遇到过这种情况吗?如何引用普通 JS 错误?

谢谢

最佳答案

您缺少 namespace 声明。试试这个:

# Foo.thrift file content
namespace js Foo
...
struct Error {
    1: i32 code,
    2: string message
}
...

那么你的 thrift 对象将是 Foo.Error

关于javascript - Thrift Node JavaScript 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15801057/

相关文章:

javascript - 如何使用 jQuery 解析字符串/URL

html - 如何在 HTML 上使用 node.js 和 socket.io.js 显示已连接的客户端

javascript - 无法使用 Node.js 和 Express 捕获 POST 参数

node.js - Slack bot - 防止机器人回复每个传入的 webhook

javascript - JS 小部件(使用 Jquery)与主机页面上的原型(prototype)冲突

svn - Subversion 合并后树冲突 - "local delete, incoming edit upon merge"

svn - 我的 svn 状态显示 "C"。我该如何解决?

javascript - jquery选择器语法问题

javascript - JavaScript 有文字字符串吗?

javascript - 为尚未附加的元素绑定(bind) "insertion into DOM event"