ReferenceError: Can not find a variable with SpookyJS

advertisements

I try to call an external function in SpookyJS by doing the same thing than in the wiki: https://github.com/WaterfallEngineering/SpookyJS/wiki/Introduction

But when I try the following code, I have this error:

ReferenceError: Can't find variable: 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(/ \- .*/, ''));
      }
    });
}

These two following logs work:

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

But the third one is responsible for the error message:

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

Any suggestion?


I would like to comment on your post instead of going with a big answer but I do not have the reputation for it, meh.

You can not pass functions in the ashing. If you were to do

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

That would work. If you want to pass an object or array, use JSON.stringify and rebuild in the casper scope.

If you want to access spooky functions from the casper scope, use event emitters instead, as following (See mostly the last lines):

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.')
}