我似乎遇到了 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"));
在回调中,它显示我有一个带有 code
和 message
的对象,而不是一个包含“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/