Javascript 从承诺链中的断言捕获错误

Javascript 从承诺链中的断言捕获错误,javascript,node.js,exception-handling,Javascript,Node.js,Exception Handling,嗨,我正在尝试捕捉承诺链中的错误,如: it("Exception is thrown for Invalid Candidate",function(){ return Election.deployed().then(function(instance){ electionInstance = instance; candidateId = 99; return electionInstance.vote(cand

嗨,我正在尝试捕捉承诺链中的错误,如:

it("Exception is thrown for Invalid Candidate",function(){
      return Election.deployed().then(function(instance){
           electionInstance = instance;
           candidateId = 99;
           return electionInstance.vote(candidateId,{from:accounts[1]});
      }).then(assert.fail).catch(function(error){
           assert(error.message.indexOf('revert') => 0,"error message must contain revert");
           return electionInstance.candidates(1);
      }).then(function(candidate1){
           var voteCount = candidate1[0];
           assert.equal(voteCount,1,"candidate1 did not recieve any votes");
           return electionInstance.candidates(2);
      }).then(function(candidate2){
           var voteCount = candidate2[0];
           assert.equal(voteCount,0,"Candidate2 didnot recieve any votes");
      });
 });
但是我在error.message附近发现了一个语法错误。 在chaijs文档中找不到任何有用的内容。 我的方法错了吗?我该怎么做? 错误如下

/home/chance/Ethereum_Work/VotingApplication/test/election.js:48
           assert(error.message.indexOf('revert') => 0,"error message must contain revert");
                       ^


SyntaxError: Unexpected token .
at new Script (vm.js:51:7)
at createScript (vm.js:138:10)
at Object.runInThisContext (vm.js:199:10)
at Module._compile (module.js:624:28)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at /home/linuxbrew/.linuxbrew/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:231:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/home/linuxbrew/.linuxbrew/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:228:14)
at Mocha.run (/home/linuxbrew/.linuxbrew/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:514:10)
at /home/linuxbrew/.linuxbrew/lib/node_modules/truffle/build/webpack:/~/truffle-core/lib/test.js:125:1
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:160:7)
/home/chance/Ethereum_Work/VotingApplication/test/election.js:48
断言(error.message.indexOf('revert')=>0,“错误消息必须包含revert”);
^
SyntaxError:意外标记。
在新脚本中(vm.js:51:7)
在createScript上(vm.js:138:10)
在Object.runInThisContext(vm.js:199:10)
在模块处编译(Module.js:624:28)
在Object.Module.\u extensions..js(Module.js:671:10)
在Module.load(Module.js:573:32)
在tryModuleLoad时(module.js:513:12)
在Function.Module.\u加载(Module.js:505:3)
at Module.require(Module.js:604:17)
根据需要(内部/module.js:11:18)
在/home/linuxbrew/.linuxbrew/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:231:27
在Array.forEach()处
位于Mocha.loadFiles(/home/linuxbrew/.linuxbrew/lib/node_modules/truffle/node_modules/Mocha/lib/Mocha.js:228:14)
在Mocha.run(/home/linuxbrew/.linuxbrew/lib/node_modules/truffle/node_modules/Mocha/lib/Mocha.js:514:10)
在/home/linuxbrew/.linuxbrew/lib/node_modules/truffle/build/webpack://~/truffle core/lib/test.js:125:1
在
在进程中。_tick回调(内部/process/next_tick.js:160:7)
此代码:

error.message.indexOf('revert') => 0
是无效的Javascript表达式,因为
=>
用于表示胖箭头回调的声明,而这不是放置回调的正确位置,因此导致错误

你是这个意思吗

error.message.indexOf('revert') === 0
或者这个:

error.message.indexOf('revert') >= 0

那么,如果你不把信息放在下面,会发生什么呢

error.toString().indexOf('revert') >= 0
这将以正确的输出成功通过测试

资料来源如下:

MARCO BARBERO于2018年1月15日发布

有时在TypeScript上,当调用indexOf()方法时,可能会出现错误“indexOf不是函数”。当变量中包含的数据由一个没有其他字符的数字表示时,即使变量已声明为字符串,也会发生这种情况

要解决此错误,应使用toString()方法:


“但是我在错误附近发现了一个语法错误。消息”真的吗?可能是因为语法确实无效。请发布错误消息。如果错误上没有属性
message
,它将在尝试调用
indexOf
时爆炸,我们很乐意帮助您理解语法错误,但您必须与我们共享语法错误的确切内容,它发生在哪一行代码上,然后我们可能需要查看语法错误中涉及的变量包含哪些内容。
someAction()
拒绝了哪些内容?我假设某个对象具有消息属性?你的语法错误是怎么说的?我猜他的意思是
=
@Bergi-猜得好。我补充说这是一种可能性。是的,嗜睡编码是一种毒药谢谢
myStringVar.toString().indexOf('mysubstring');