English 中文(简体)
节点 REPL eval 回召
原标题:Node REPL eval Callback

我有一个小样本程序 < a href=> "http://pastebin.com/5gFkaPgg" rel="nofollow" >http://pastebin.com/5gFkaPgg ,为TCP的客户端提供REPL服务。根据文件 < a href=>http://nodejs.org/api/repl.html" rel="nofolpol" >http://nodejs.org/api/repl.html ,我通过 eval 功能(第11-13行) 设置是正确的, 但回拨对象不是一个函数。我在 docs 错误解释什么?

  callback(null,result);
  ^
TypeError: object is not a function

无法回答我的问题...

According to https://github.com/joyent/node/blob/master/lib/repl.js the signature is

function(code, context, file, cb) {
  //code
  cb(err, result);
}

如果有更合适的解决办法,请通知我。

最佳回答

错误和签名告诉我,回调的理由是其参数 (代码、上下文、文件、cb) ,但其参数 (代码、cb) (代码、cb) (代码),因此 context > cb 的约束,而且由于 context 不是函数,因此产生了错误。

您需要做的是将给 repl. start 回调的参数列表更改为 :

function(cmd, context, file, callback) {

或使用公号:

function(cmd) {
  var callback = arguments[arguments.length-1]; // get the last argument

代码为第二个选项,因为它不引入新名称:

var net = require( net );
var repl = require( repl );

function main() {
  var clients = [];

  net.createServer(function(socket) {
    clients.push(socket);

    repl.start(">", socket, function(cmd) {
      var callback = arguments[arguments.length-1];
      var result = cmd;

      callback(null,result);
    });
    socket.on( end ,function() {
      clients.splice(clients.indexOf(socket));
    });
  }).listen(8000);
}

main();
问题回答

暂无回答




相关问题
Is there assembler REPL under linux?

Recently I ve started plaing with assembler under linux, there s good debuger, but comming from Ruby I m missing simple REPL that would let me enter a line of assembler code and see the result on ...

C# REPL tools; quick console-like compiling tool

Often times, I start a new instance of Visual Studio, just to create a console application that has some output and/or input. It s a temporary sandbox I use to test a method or something else and ...

How to play with Specs matchers in Scala REPL?

While debugging or exploring spec features it would be more advantageous to type them in REPL (Scala interpreter) rather then in file with spec and run it with something like maven. What is the ...

octave: load many functions from single file

How can I put multiple functions in one file and later get access to all of them in the octave interpreter ? I don t want to have a thousand files and want to group functions together. I d like ...

Clojure emacs slime + swank directory question

I m using emacs with clojure-swank and slime and trying to set my development environment. And I ran into a problem. When I start a repl I m stuck in an unknown directory preventing me to load my ...

scala: tracing implicits selection and other code magics

When trying to figure how a library works, implicit conversions are confusing. For example, looking at an expression like val foo: Foo = 1 , what converts 1 to Foo? Is it possible to instruct the ...

热门标签