节俭节点javascript命名空间

thrift node javascript namespace

本文关键字:命名空间 javascript 节点      更新时间:2023-09-26

我似乎正在经历节点/节俭名称空间冲突。

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) {
...

我将模块包含在节点

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

我似乎遇到了一个名称空间冲突与javascript的错误对象

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

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

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

还有人遇到过这个吗?我如何参考普通JS错误?

谢谢

您缺少名称空间声明。试试这个:

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

那么你的节俭对象将是Foo.Error