1

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?

Jean Lebrument
  • 5,079
  • 8
  • 33
  • 67

3 Answers3

1

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.') 
}
Kevin Gagnon
  • 141
  • 11
0

Clearly, you are using variable test

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

without declaring it first.

var test;

Replace it with req maybe. :)

Vasyl Boroviak
  • 5,959
  • 5
  • 51
  • 70
  • Yes I did declare the variable `test`, look at this line: ```spooky.then([{ test: visitUrl }, function(){``` I found this from the documentation that I posted in my original post. – Jean Lebrument Mar 18 '15 at 03:54
  • This is not variable declaration. You created an array `[]`, put new object inside `{}`, the object has a property `test`. It's not a variable, but a property of anonymous object. – Vasyl Boroviak Mar 18 '15 at 03:58
  • Variables should either be declared using `var` keyword, or declared as function parameters: `function(urlIndex, nbClicked)` – Vasyl Boroviak Mar 18 '15 at 03:59
  • Yes I know, but this is from the documentation that I posted on my original post. I tested by replacing `test` by `visitUrl` in the console.log: ```console.log("THIRD: " + visitUrl + " \n\n\n END THIRD");``` and I still have the error message: `ReferenceError: Can't find variable: visitUrl` – Jean Lebrument Mar 18 '15 at 06:10
  • Mate, you have to learn what variable and its function scope is. You don't have visitUrl at this place. Try removing the variable usage from the erroring line. – Vasyl Boroviak Mar 18 '15 at 06:30
  • I understand, but I definitely need to use this variable in this place. So my question is how could I do this? – Jean Lebrument Mar 18 '15 at 16:18
0

Finally I succeed to do what I needed by using PhantomJS-node instead of SpookyJS.

Jean Lebrument
  • 5,079
  • 8
  • 33
  • 67