javascript - PhantomJS 错误 : TypeError: undefined is not a constructor (evaluating 'require(' system'). 创建()')

标签 javascript ruby-on-rails phantomjs

更新::问题已解决,我能够将其专门隔离到我的 JavaScript 文件中。

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

这是我的代码:

条目 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 向我发出警告“未解析的变量或类型幻像”。

想法?

最佳答案

对于那些遇到过 TypeError: undefined is not a constructor 问题的人,对于我的问题,我不小心有这样一行:

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

当它应该是

var system = require('system')

关于javascript - PhantomJS 错误 : TypeError: undefined is not a constructor (evaluating 'require(' system'). 创建()'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39651987/

相关文章:

javascript - 根据需要,从 jquery 事件有选择地关闭各个子 python 进程

ruby-on-rails - Rails Devise - 为登录用户和匿名用户定义单独的根路径

ruby-on-rails - 更改设备默认布局

ruby-on-rails - 在 Mongoid 中设置嵌入字段值的最简单方法是什么?

python - 如何为 PhantomJS 设置超时?

javascript - 从本地 js 脚本对本地 Phantom.js 服务器的 Ajax 调用

javascript - jQuery 对话框模式在单击时被调用两次

javascript - React Native 上的 DeviceOrientation 事件

javascript - Grunt 服务器错误,EMFILE

angularjs - Protractor 测试在 Chrome/Firefox 中完成,但在 phantomjs 中失败