我尝试通过做与wiki相同的事情来调用SpookyJS中的外部函数:https://github.com/WaterfallEngineering/SpookyJS/wiki/Introduction
但是当我尝试以下代码时,我有这个错误:
ReferenceError:找不到变量:test
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");
有什么建议吗?
答案 0 :(得分:1)
我想对你的帖子发表评论,而不是给出一个很好的答案,但我没有它的声誉,嗯。
您无法在灰化中传递函数。如果你要做
var x = 'HELLO'
spooky.then([{
XinCasper : x
}, function(){
//do something with XinCasper
}])
那会有用。如果要传递对象或数组,请在casper范围内使用JSON.stringify并重建。
如果你想从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.')
}
答案 1 :(得分:0)
显然,您使用的是变量test
console.log("THIRD: " + test + " \n\n\n END THIRD");
没有先声明。
var test;
可能用req
替换它。 :)
答案 2 :(得分:0)
最后,我成功地使用PhantomJS-node而不是SpookyJS来做我需要的。