Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 无法设置';名称';引发异常时未定义的_Node.js_Thrift - Fatal编程技术网

Node.js 无法设置';名称';引发异常时未定义的

Node.js 无法设置';名称';引发异常时未定义的,node.js,thrift,Node.js,Thrift,我只是在nodejs中尝试Apache-Thrift,然后在即将到来的项目中使用它,在这个项目中我遇到了这个错误 这是我的demo.thrift文件 namespace js demo typedef i32 int enum Operation { ADD = 1, SUBTRACT = 2, MULTIPLY = 3, DIVIDE = 4 } struct Work { 1: int num1 = 0, 2: int num2, 3: Operation

我只是在nodejs中尝试Apache-Thrift,然后在即将到来的项目中使用它,在这个项目中我遇到了这个错误

这是我的
demo.thrift
文件

namespace js demo

typedef i32 int

enum Operation {
  ADD = 1,
  SUBTRACT = 2,
  MULTIPLY = 3,
  DIVIDE = 4
}

struct Work {
  1: int num1 = 0,
  2: int num2,
  3: Operation op,
  4: optional string comment
}

exception InvalidOperation {
  1: int message,
  2: string trace
}

service Calculator {

  void ping()

  double calculate(1: int logid, 2: Work w) throws (1: InvalidOperation oops),

  oneway void zip()
}
这是
server.js的一部分

// inside thrift.createServer
calculate: (logid, work, result) => {
let answer = null, oops = null;
switch(work.op) {
// Code related to Operation.ADD, Operation.SUBTRACT ...
    default: {
      console.log("ERROR!");
      oops = InvalidOperation();
      oops.message = work.op;
      oops.trace = "Unknown Operation";
   }
}
   result(oops, answer);
}
我使用switch case来确定
server.js中的操作

// inside thrift.createServer
calculate: (logid, work, result) => {
let answer = null, oops = null;
switch(work.op) {
// Code related to Operation.ADD, Operation.SUBTRACT ...
    default: {
      console.log("ERROR!");
      oops = InvalidOperation();
      oops.message = work.op;
      oops.trace = "Unknown Operation";
   }
}
   result(oops, answer);
}
client.js
使用
calculate(12345,{num1:1,num2:2,op:10})调用服务器时

而不是返回它抛出的错误
TypeError:无法设置演示类型中未定义的属性“name”。js:122

demo_types.js
中与无效操作相关的部分是

// Work related code
var InvalidOperation = module.exports.InvalidOperation = function(args) {
  Thrift.TException.call(this, "InvalidOperation");
  this.name = "InvalidOperation"; // points to here
  this.message = null;
  this.trace = null;
  if (args) {
    if (args.message !== undefined && args.message !== null) {
      this.message = args.message;
    }
    if (args.trace !== undefined && args.trace !== null) {
      this.trace = args.trace;
    }
  }
};
Thrift.inherits(InvalidOperation, Thrift.TException);
InvalidOperation.prototype.name = 'InvalidOperation';
// InvalidOperation.read & .write

你知道为什么会抛出这个错误吗?

事实上,我知道为什么会抛出这个错误。这是一个明显的老Javascript错误

oops=新的无效操作()

就这样。

(1)你可以接受自己的答案。(2) 这是您的代码中的一个bug还是节俭代码(生成的还是库的)?这绝对是一个“我”bug。这个回答节省了我面对面的时间。