2016-09-23 17 views
1

UPDATE ::問題が解決しました、私はJAVASCRIPTファイルを特に隔離することができました。PhantomJSエラー:TypeError:未定義がコンストラクタではありません( 'require(' system ')。create()')

cap_screen.js

var page = require('webpage').create(); //Create a new instance of a web page 
var system = require('system').create(); //Our script needs to require Phantom's Web Page module 


page.onError = function(msg, trace) { //Our script needs to require Phantom's Web Page module 

    var msgStack = ['ERROR: ' + msg]; 

    if (trace && trace.length) { 
     msg.push('TRACE:'); 
     trace.forEach(function(t) { 
      msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : '')); 
     }); 
    } 

    console.error(msgStack.join('\n')); 
}; 

//Now write core of screen cap script 
//Remember: system.args[1] = "http://wwww.clowder.com" system.args[2] = "clowder-pic.png" 
page.open(system.args[1], function(status) { 
    console.log('Status: ' + status); 
    page.render(system.args[2]); //this line captures the screen 
    phantom.exit(); 
}); 

私の問題は、URLを提出する際に、次のエラーがポップアップ表示される:

TypeError: undefined is not a constructor (evaluating 'require('system').create()') 
    phantomjs://code/cap_screen.js:10 in global code 

は、これは私のコードです:

def create 
    @entry = Entry.new(entry_params) 
    @entry.image = cap_screen 
    if @entry.save 
     redirect_to root_path 
    else 
     render('index') 
    end 
    end 
    private 

    PATH_TO_PHANTOM_SCRIPT = Rails.root.join('app', 'assets', 'javascripts', 'cap_screen.js') 
    def cap_screen 
    Dir.chdir(Rails.root.join('public', 'images')) 
    system "phantomjs #{PATH_TO_PHANTOM_SCRIPT} #{params['entry_url']} # {params['entry_url']}.png" 
    end 

    def entry_params 
    params.require(:entry).permit(:title, :url) 
    end 

entries_controller

私のcap_screen.jsファイルで、私のIDEが私に "Unresolved variable or type phantom"という警告を表示します。

思考?私の問題のために、私は誤ってラインを持っていたTypeError: undefined is not a constructorに問題があった人のために

答えて

1

、:

var system = require('system').create();

それがである場合、それは

var system = require('system')

0

されている必要があります仕様ファイル、プロバイダークラスでcreate()関数があることを確認してください(以下の例では、MockServiceに関数が必要です)。

{ 
provide: .....Service, 
useClass: MockService 
} 

export class MockService { 
create() { 
// your code for test 
} 
constructor() { } 
} 
関連する問題