1

我尝试通过在 wiki 中执行相同的操作来调用 SpookyJS 中的外部函数:https ://github.com/WaterfallEngineering/SpookyJS/wiki/Introduction

但是当我尝试下面的代码时,我有这个错误:

ReferenceError:找不到变量:测试

try {
    var Spooky = require('spooky');
} catch (e) {
    var Spooky = require('../lib/spooky');
}

var urls = ["http://www.google.fr",
            "http://www.yahoo.com"
          ];

exports.clicker = function(req, res)
{
  console.log("FIRST: " + visitUrl + " \n\n\n END FIRST");


  var visitUrl = function(urlIndex, nbClicked)
  {
      console.log("HELLO");
  };

  var spooky = new Spooky(
    {
      child: {
        // transport: 'http'
      },
      casper: {
        logLevel: 'debug',
        verbose: true
      }
    }, function (err)
    {
      if (err)
      {
        e = new Error('Failed to initialize SpookyJS');
        e.details = err;
        throw e;
      }

      spooky.start(urls[0]);

      console.log("SECOND: " + visitUrl + " \n\n\n END SECOND");

      spooky.then([{
        test: visitUrl
      }, function(){

        console.log("THIRD: " + test + " \n\n\n END THIRD");
      }]);

      spooky.run();
    });

    // Uncomment this block to see all of the things Casper has to say.
    // There are a lot.
    // He has opinions.
    spooky.on('console', function (line) {
      console.log(line);
    });

    spooky.on('hello', function (greeting) {
      console.log(greeting);
    });

    spooky.on('log', function (log) {
      if (log.space === 'remote') {
        console.log(log.message.replace(/ \- .*/, ''));
      }
    });
}

以下两个日志有效:

console.log("FIRST: " + visitUrl + " \n\n\n END FIRST");
console.log("SECOND: " + visitUrl + " \n\n\n END SECOND");

但是第三个负责错误消息:

console.log("THIRD: " + test + " \n\n\n END THIRD");

有什么建议吗?

4

3 回答 3

1

我想对你的帖子发表评论,而不是给出一个大答案,但我没有这方面的声誉,嗯。

你不能在灰化中传递函数。如果你要做

var x = 'HELLO'
spooky.then([{
    XinCasper : x
}, function(){
    //do something with XinCasper
}])

那会奏效。如果要传递对象或数组,请使用 JSON.stringify 并在 casper 范围内重建。

如果您想从 casper 范围访问诡异的函数,请改用事件发射器,如下所示(主要参见最后几行):

try {
var Spooky = require('spooky');
} catch (e) {
    var Spooky = require('../lib/spooky');
}

var urls = ["http://www.google.fr",
        "http://www.yahoo.com"
      ];

exports.clicker = function(req, res)
{
console.log("FIRST: " + visitUrl + " \n\n\n END FIRST");


var visitUrl = function(urlIndex, nbClicked)
{
  console.log("HELLO");
};

var spooky = new Spooky(
{
  child: {
    // transport: 'http'
  },
  casper: {
    logLevel: 'debug',
    verbose: true
  }
}, function (err)
{
  if (err)
  {
    e = new Error('Failed to initialize SpookyJS');
    e.details = err;
    throw e;
  }

  spooky.start(urls[0]);

  console.log("SECOND: " + visitUrl + " \n\n\n END SECOND");

  spooky.then(function(){
      //casper scope
      var y = 'something'
      this.emit('third', y)
  });

  spooky.run();
});

spooky.on('third', function(y){
    console.log('Hey, I can output ' + y + ' here.') 
}
于 2015-11-26T17:08:38.707 回答
0

显然,您正在使用变量test

console.log("THIRD: " + test + " \n\n\n END THIRD");

没有先声明。

var test;

换成req也许吧。:)

于 2015-03-18T01:31:30.473 回答
0

最后,我通过使用PhantomJS-node而不是 SpookyJS成功地完成了我需要的工作。

于 2015-03-18T21:12:30.737 回答