Vorpal.js参数和参数

我正在开发一个Node.js应用程序,我正在使用Vorpal命令。 我试图从命令发送一个值到一个函数,但我不能得到它的工作。 我做错了什么?

这里是代码:

vorpal .command('rollto <num>', 'Rolls to') .action(function(num) { rollto(num); }); function rollto(num) { bettime = bettimems % 60; socket.emit('betting', bettime); timer1 = setInterval(function () { bettime--; socket.emit('betting', bettime); if (bettime == 0) { socket.emit('random number', num); console.log("Rolled to:" + num + "!!!"); clearInterval(timer1); } }, 1000); } 

问题是你传递给命令的action的函数有不同的参数。

以下是文档中的相关部分:

 .command.action(function) This is the action execution function of a given command. It passes in an arguments object and callback. Actions are executed async and must either call the passed callback upon completion or return a Promise. 

这是一个工作的例子:

 var vorpal = require('vorpal')(); vorpal .command('rollto <num>', 'Rolls to') .action(function(arguments, callback) { rollto(arguments, callback); }); function rollto(arguments, callback) { var num = arguments.num; // get 'num' parameter from arguments timer1 = setInterval(function () { console.log('test'); console.log(num); clearInterval(timer1); callback(); // Don't forget to use callback() to notify vorpal }, 1000); } vorpal .delimiter('myapp$') .show(); 

注意你实际上在setInterval里面有一个asynchronous代码,所以你需要在最后使用callback()来通知vorpal处理完成。