PhantomJS错误:TypeError: undefined不是构造函数(求值'require('s

PhantomJS error: TypeError: undefined is not a constructor (evaluating 'require('system').create()')

本文关键字:求值 require 构造函数 TypeError 错误 undefined PhantomJS      更新时间:2023-09-26

更新::问题解决了,我能够将它隔离到我的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

这是我的代码:

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给了我警告"未解决的变量或类型幻影"。

想法吗?

对于那些对TypeError: undefined is not a constructor有问题的人,对于我的问题,我不小心写了一行:

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

当它应该是

var system = require('system')

如果它在Spec文件中,请确保在提供者类中有create()函数(在下面的示例中MockService需要该函数)。

{
provide: .....Service,
useClass: MockService
}
export class MockService {
create() {
// your code for test
}
constructor() { }
}

我有同样的错误,但对于不同的字符串函数。

TypeError: undefined不是构造函数(求值'inputValue.replace(/[^0-9a-zA-Z,]/g, ")')

我的修复是添加". tostring ()"之间的变量和"。替换"方法调用"。我把我的代码包含在下面以供进一步参考。

(function () {
    'use strict';
    angular
        .module('mo.pages.global-search.directives')
        .directive('extentionListValidate', restrictTextfieldToAlphaNumericList);
function restrictTextfieldToAlphaNumericList() {
    return {
        require: 'ngModel',
        link: function (scope, element, attrs, modelCtrl) {
            modelCtrl.$parsers.push(function (inputValue) {
                var transformedInput = inputValue ? inputValue.toString().replace(/[^0-9a-zA-Z,]/g, '') : null;
                if (transformedInput != inputValue) {
                    modelCtrl.$setViewValue(transformedInput);
                    modelCtrl.$render();
                }
                return transformedInput;
            });
        }
    };
}
})();