如何在nodejs中将Parameter传递给spooky



我正试图将参数表countryCode发送到spookyjs函数。

我的问题是如何做到这一点,因为当我想使用countryCode在诡异内部。然后(函数)country Code为空

非常感谢

这是呼叫代码

var greetings = require("./get_country.js");
var countryCode = "ES";
greetings.getOne("ES");

这是功能代码:

var Spooky = require('spooky');
module.exports = {
  getOne: function(countryCode) {
    var init = function(error) {
      if (error) {
        e = new Error('Failed to initialize SpookyJS');
        e.details = error;
        throw e;
      }
      spooky.start('http://www.domain.com/');
        spooky.then(function() {
          this.emit('return', this.evaluate(function() {
            var raw_countries = document.querySelectorAll('#country-selector li.' + countryCode);
            return raw_countries;
          }));
       });
       spooky.run();
       },
       spooky = new Spooky({
         child: {
         transport: 'http'
       },
       casper: {
         logLevel: 'debug',
         verbose: true
       }
    }, init);
    spooky.on('error', function(error, stack) {
        console.error(error);
        if (stack) {
            console.log(stack);
        }
    });
    spooky.on('return', function(data) {
        console.log(data);
    });
}};

您需要将所需的全局变量传递给spooky的then()和evaluate()函数,才能访问它,如下所示

    spooky.start('example.com/');
    spooky.then([{ countryCode: countryCode }, function () {
        this.emit('return', this.evaluate(function (countryCode) {
            var raw_countries = document.querySelectorAll('#country-selector li.' + countryCode);
            return raw_countries;
        }), countryCode);
    }]);
    spooky.run();

参考示例

最新更新