【问题标题】:How to pass Parameter to spooky in nodejs如何在nodejs中将参数传递给幽灵
【发布时间】:2023-03-24 19:05:01
【问题描述】:

我正在尝试将参数 countryCode 发送到 spookyjs 函数。

我的问题是如何做到这一点,因为当我想使用 countryCode 在 spooky.then(function) 中 countryCode 为空

非常感谢

这是调用代码

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);
    });
}};

【问题讨论】:

  • 什么是空的或错的?
  • @webduvet 在这一行 var raw_countries = document.querySelectorAll('#country-selector li.' + countryCode); var countryCode 为空。

标签: node.js variables parameters spookyjs


【解决方案1】:

您需要将所需的全局变量传递给 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();

Reference example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多