2016-09-23 86 views
1

更新::問題已解決,我可以專門將其隔離到我的JavaScript文件。PhantomJS錯誤:TypeError:undefined不是一個構造函數(評估'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 

這是我的代碼:

entries_controller

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 

在我的cap_screen.js文件中,我的IDE給了我一個警告「未解析變量或類型幻像」。

想法?

回答

1

對於誰曾問題TypeError: undefined is not a constructor,我的問題的人,我不小心有行:

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() { } 
} 
相關問題