未处理的拒绝错误 - horseman

Unhandled rejection Error - horseman

我正在尝试 运行 来自 horsemanjs 的示例代码稍作修改:

var Horseman = require('node-horseman');
var horseman = new Horseman();
horseman
    .userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0')
    .open('http://www.google.com')
    .type('input[name="q"]', 'github')
    .click('[name="btnK"]')
    .keyboardEvent('keypress', 16777221)
    .waitForSelector('div.g')
    .plainText()
    .log()
    .close();

几天前它确实起作用了,但突然决定不再起作用了。当我 运行 它时我得到了这个:

➜ Debug=horseman node test.js
   horseman using PhantomJS from phantomjs-prebuilt module +0ms
   horseman .setup() creating phantom instance 1 +3ms
   horseman phantom created +195ms
   horseman .close(). +13ms
Unhandled rejection Error
      at IncomingMessage.<anonymous(/home/halvor/BettingSource/node_modules/node-phantom-simple/node-phantom-simple.js:612:14)
      at IncomingMessage.emit (events.js:188:7)
      at endReadableNT (_stream_readable.js:974:12)
      at _combinedTickCallback (internal/process/next_tick.js:80:11)
      at process._tickCallback (internal/process/next_tick.js:104:9)

使用 Bluebird 调试:

➜  BettingSource DEBUG=horseman BLUEBIRD_DEBUG=1 node test.js                                                                             
    horseman using PhantomJS from phantomjs-prebuilt module +0ms
    horseman .setup() creating phantom instance 1 +3ms
    horseman phantom created +149ms
    horseman .close(). +16ms
Unhandled rejection HeadlessError: Error parsing JSON from phantom: SyntaxError: Unexpected end of JSON input
Data from phantom was:
    at IncomingMessage.<anonymous> (/home/halvor/BettingSource/node_modules/node-phantom-simple/node-phantom-simple.js:612:14)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:188:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)
From previous event:
    at prepare (/home/halvor/BettingSource/node_modules/node-horseman/lib/index.js:54:9)
    at new Horseman (/home/halvor/BettingSource/node_modules/node-horseman/lib/index.js:155:15)
    at Object.<anonymous> (/home/halvor/BettingSource/test.js:2:16)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:425:7)
    at startup (bootstrap_node.js:146:9)
    at bootstrap_node.js:540:3

Phantomjs 从终端运行,我尝试重新安装节点模块。我必须指出,我对节点的经验很少。

注意:要安装 phantomjs/horseman,我执行了以下操作:

  1. 从 phantomjs 网站下载 tar.gz 文件并解压,将其放入 bath /home/bin
  2. 运行 npm install phantom /home/halvor/BettingSource
  3. 运行 npm 安装 phantomjs
  4. 运行 npm install node-horseman

两次我安装 phantomjs 时都说在路径中找不到 phantomjs 并再次下载。

看起来错误来自同一目录中的 .htpasswd 文件。删除后一切正常

您需要通过以下方式处理 promise 拒绝:

.catch(err => {
    // handle the error here
});

对于在您的程序中创建的每个承诺。否则它将兑现承诺链中的任何拒绝。

有关详细信息,请参阅此答案: