2017-05-18 1 views
0

私は流星プロジェクトに 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); 
      }); 
    } 
}) 

このコードは(Iボタンをクリックします)から呼び出されたとき、私は例外を取得:

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でそれを実行すると、それ自身のプロジェクトでうまく動作します。

答えて

0

Nightmareは、ブラウザで直接実行されるのではなく、ノード上で実行されるはずです。

Template.template_name.events内のコードはクライアントのブラウザで実行されるため、エラーメッセージが表示されます。

ナイトメアコードをサーバー専用のメソッドに移動し、そのメソッドをイベントハンドラで呼び出すことで、何をしようとしているのかを試すことができます。