Javascript Cron与噩梦js

Javascript Cron与噩梦js,javascript,cron,nightmare,Javascript,Cron,Nightmare,我正在尝试在本地运行一个带有噩梦js的cron。 不幸的是,我有这个错误 Unhandled rejection (<{"message":"navigation error","code":-...>, no stack trace) 下面是噩梦的样子: var Nightmare = require('nightmare'); var nightmare = Nightmare({ typeInterval: 300, show: true }); nightmare

我正在尝试在本地运行一个带有噩梦js的cron。 不幸的是,我有这个错误

Unhandled rejection (<{"message":"navigation error","code":-...>, no stack trace)
下面是噩梦的样子:

var Nightmare = require('nightmare');
var nightmare = Nightmare({
  typeInterval: 300,
  show: true
});

nightmare
  .goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
  .type('[name=email]', '')
  .wait(1000)
  .type('[name=email]', 'myemail')
  .wait(1000)
  .type('[name=password]', '')
  .wait(1000)
  .type('[name=password]', 'mypassword')
  .click('[type=submit]')
  .wait(25000)
  .wait(25000)
  .evaluate(function (page, done) {

    document.documentElement
    done()
  })
  .end()
  .then(function (result) {
    // fs.writeFileSync('testOutput.json', JSON.stringify(result));
    console.log(JSON.stringify(result))
  })
  .catch(function (error) {
    console.error('failed:', error);
  });

当我在没有cron的情况下运行函数crawl时,它工作得非常好

好吧,一开始我就不确定我是否正确,因为我没有太多的经验,而且您也没有在cron中指定您定义的内容。但是从我的快速搜索中,你的猜测是正确的。使用cron时,通过命令行进行调用。现在的噩梦是建立在电子上的,而电子又依赖于铬。据我所知,Electron可能有一个bug,每次在真正的chromium浏览器上立即加载页面时都会导致超时。因此,从我目前收集的信息来看,你的应用程序需要与铬进行电子通信才能正常工作,而在你的情况下,它似乎无法正常工作。我很抱歉说得含糊不清,可能是错的,但我只能提供这么少的信息。

我的问题在于cron的设置。 我宁愿使用

var job = new CronJob('* 10 * * * *', function() {
    crawl()
  }, function () {
    console.log("crawl ended")
  },
  true
);
此外,我还必须在我的函数中重新定义噩梦设置

var get_data = function(){
  var Nightmare = require('nightmare');
  var nightmare = Nightmare({
    typeInterval: 300,
    show: true
  });
  nightmare
  .goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
  .type('[name=email]', '')
  .wait(1000)
  .type('[name=email]', 'myemail')
  .wait(1000)
  .type('[name=password]', '')
  .wait(1000)
  .type('[name=password]', 'mypassword')
  .click('[type=submit]')
  .wait(25000)
  .wait(25000)
  .evaluate(function (page, done) {

    document.documentElement
    done()
  })
  .end()
  .then(function (result) {
    // fs.writeFileSync('testOutput.json', JSON.stringify(result));
    console.log(JSON.stringify(result))
  })
  .catch(function (error) {
    console.error('failed:', error);
  });
}
而不是

var Nightmare = require('nightmare');
var nightmare = Nightmare({
  typeInterval: 300,
  show: true
});

var get_data = function(){
  nightmare
  .goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
  .type('[name=email]', '')
  .wait(1000)
  .type('[name=email]', 'myemail')
  .wait(1000)
  .type('[name=password]', '')
  .wait(1000)
  .type('[name=password]', 'mypassword')
  .click('[type=submit]')
  .wait(25000)
  .wait(25000)
  .evaluate(function (page, done) {

    document.documentElement
    done()
  })
  .end()
  .then(function (result) {
    // fs.writeFileSync('testOutput.json', JSON.stringify(result));
    console.log(JSON.stringify(result))
  })
  .catch(function (error) {
    console.error('failed:', error);
  });
}

谢谢你帮我解决这么一个模糊的问题+谢谢你的帮助,阿亚斯卡特。“我将尝试与幻影JS合作。”昆廷德尔的噩梦是巨大的superior@naomik谢谢你的提示,我问了一个更准确的问题:
var Nightmare = require('nightmare');
var nightmare = Nightmare({
  typeInterval: 300,
  show: true
});

var get_data = function(){
  nightmare
  .goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
  .type('[name=email]', '')
  .wait(1000)
  .type('[name=email]', 'myemail')
  .wait(1000)
  .type('[name=password]', '')
  .wait(1000)
  .type('[name=password]', 'mypassword')
  .click('[type=submit]')
  .wait(25000)
  .wait(25000)
  .evaluate(function (page, done) {

    document.documentElement
    done()
  })
  .end()
  .then(function (result) {
    // fs.writeFileSync('testOutput.json', JSON.stringify(result));
    console.log(JSON.stringify(result))
  })
  .catch(function (error) {
    console.error('failed:', error);
  });
}