【问题标题】:Nightmare with meteor throws an exception流星的噩梦引发异常
【发布时间】:2017-05-18 12:29:46
【问题描述】:

我安装了噩梦 npm install nightmare --save 在我的流星项目中。 然后我尝试在模板辅助方法中运行噩梦示例代码:

Template.nighmare.events({
  "click #doSomething"() {

    let Nightmare = require('nightmare');
    let nightmare = Nightmare({ show: true });

    nightmare
      .goto('https://duckduckgo.com')
      .type('#search_form_input_homepage', 'github nightmare')
      .click('#search_button_homepage')
      .wait('#zero_click_wrapper .c-info__title a')
      .evaluate(function () {
        return document.querySelector('#zero_click_wrapper .c-info__title a').href;
      })
      .end()
      .then(function (result) {
        console.log(result);
      })
      .catch(function (error) {
        console.error('Search failed:', error);
          });
    }
})

当从(我单击一个按钮)调用此代码时,我得到了异常:

Uncaught TypeError: fs.existsSync is not a function
    at meteorInstall.node_modules.electron.index.js (modules.js?hash=1ae810a…:109967)
    at fileEvaluate (modules-runtime.js?hash=0969a31…:207)
    at require (modules-runtime.js?hash=0969a31…:130)
    at meteorInstall.node_modules.nightmare.lib.nightmare.js (modules.js?hash=1ae810a…:107767)
    at fileEvaluate (modules-runtime.js?hash=0969a31…:207)
    at require (modules-runtime.js?hash=0969a31…:130)
    at Object.click #doSomething (app.js:4330)
    at blaze.js?hash=813922c…:3774
    at Function.Template._withTemplateInstanceFunc (blaze.js?hash=813922c…:3743)
    at Blaze.View.<anonymous> (blaze.js?hash=813922c…:3773)

我不知道如何解决这个问题,任何提示或提示将不胜感激!

有谁让流星+噩梦一起工作?

如果我将相同的代码放在 test.js 文件中并在其自己的项目中使用节点 test.js 运行它就可以了。

【问题讨论】:

    标签: javascript node.js exception meteor nightmare


    【解决方案1】:

    Nightmare 应该在 Node 上运行,而不是直接在浏览器中运行。

    Template.template_name.events 中的代码在客户端的浏览器上执行,因此会出现错误消息。

    您可能会尝试通过将 Nightmare 代码移动到仅限服务器的方法中来实现您想要做的事情,并在您的事件处理程序中调用该方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 2010-12-21
      • 2020-03-14
      • 2010-11-13
      • 2010-12-22
      • 1970-01-01
      相关资源
      最近更新 更多