javascript Jquery / Handlebars 错误消息 - Uncaught TypeError: Object [object Object] 没有方法“匹配”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10330129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:30:19  来源:igfitidea点击:

Jquery / Handlebars error message - Uncaught TypeError: Object [object Object] has no method 'match'

javascriptjquerymysqlajaxhandlebars.js

提问by monkeylumps

i am working on a little learning project and have run into an issue that i cant work out.

我正在做一个小学习项目,但遇到了一个我无法解决的问题。

I get the following error message on google chromes dev console :-

我在谷歌浏览器开发控制台上收到以下错误消息:-

Uncaught TypeError: Object [object Object] has no method 'match'
lexer.nexthandlebars-1.0.0.beta.6.js:364
lexhandlebars-1.0.0.beta.6.js:392
lexhandlebars-1.0.0.beta.6.js:214
parsehandlebars-1.0.0.beta.6.js:227
Handlebars.parsehandlebars-1.0.0.beta.6.js:507
compilehandlebars-1.0.0.beta.6.js:1472
(anonymous function)handlebars-1.0.0.beta.6.js:1481
(anonymous function)scripts.js:103
jQuery.Callbacks.firejquery.js:1046
jQuery.Callbacks.self.fireWithjquery.js:1164
donejquery.js:7399
jQuery.ajaxTransport.send.callback

Now this shows up at an error with the following code in the handlebars scrips

现在,这在车把脚本中显示以下代码的错误

match = this._input.match(this.rules[rules[i]]);
Uncaught TypeError: Object [object Object] has no method 'match'

So what i take from this is that there must be an issue with my code and not the handlebar code even though it is in beta.

所以我从中得到的是,即使它处于测试阶段,我的代码也一定有问题,而不是车把代码。

Here is the section of code that kicked it all off.

这是启动这一切的代码部分。

displayJobInfo: function( e ) {
    var self = Actors;

    self.config.jobInfo.slideUp( 300 );
    var jobnum = $(this).data( 'job_id' );
    $.ajax({
        data: { job_id: jobnum }

    }).then(function( results ) {
        self.config.jobInfo.html( self.config.JobInfoTemplate( { jobs: results, job_id: jobnum }) ).slideDown(300);
    });
    console.log($(this).data( 'job_id' ));
    e.preventDefault();
}

I have spent hours trying to work this one out myself and have got almost the same section of code working in another part of my site.

我花了好几个小时试图自己解决这个问题,并且在我网站的另一部分中得到了几乎相同的代码部分。

Little bit of background - i am using php to pull a database from mysql and then to query the database based on as users input and jquery to overlay the fields back onto the page.

一点背景知识 - 我正在使用 php 从 mysql 中提取数据库,然后根据用户输入和 jquery 查询数据库以将字段覆盖回页面。

回答by Leopd

This happens if you try to compile a template from a jquery element object instead of from a string. For example

如果您尝试从 jquery 元素对象而不是字符串编译模板,就会发生这种情况。例如

<script id="my-template-script" type="text/template">...</script>

and then

接着

var my_template = Handlebars.compile( $("#my-template-script") );  // WRONG

You might expect this to blow up right away, but it doesn't. Instead it should be

您可能希望这会立即爆炸,但事实并非如此。相反应该是

var my_template = Handlebars.compile( $("#my-template-script").html() );

回答by Bhanu Prakash Koppaka

If you are getting the template as text/html, then your template might be a htmlDocument. If you initialize the template as follows, then it will work fine.

如果您以 text/html 形式获取模板,那么您的模板可能是 htmlDocument。如果您按如下方式初始化模板,那么它将正常工作。

function getTemplate(templateName,context, callback, errorCallback) {
var template = {};
template.name = templateName;
$.ajax({
    url: templateName,
    timeout: 1000,
    datatype: "text/javascript",
    error: function(jqXHR, textStatus, errorThrown) {
        errorCallback && errorCallback.call(context, textStatus);
    },
    success:function(response, textStatus, jqXHR) {
        try {
            template['handlebar'] = Handlebars.compile(jqXHR.responseText);
        } catch(e) {
            console.error("error while creating Handlebars script out of template for [", template, e);
            throw e;
        }
        template['rawTemplate'] = jqXHR.responseText;
        callback && callback.call(context, template);
        return response;
    }
});

}

}

If you use the response param instead of jqHXR.responseText then you will get the "match" not found. I have tried it.

如果您使用响应参数而不是 jqHXR.responseText 那么您将找不到“匹配”。我试过了。

回答by Jérémie Parker

matchapply to string only. You have to apply it to the value of the input. If it's a jQuery object, you can use _input.val()otherwise _input.valueshould work.

match仅适用于字符串。您必须将其应用于输入的值。如果它是一个 jQuery 对象,则可以使用,_input.val()否则_input.value应该可以工作。

On the other hand, As it's part of the library, you may want to check what data type are expected as input and what you are actually sending.

另一方面,由于它是库的一部分,您可能需要检查期望作为输入的数据类型以及您实际发送的数据类型。

nullfor instance is an object in javascript, so you probably want to change it in an empty string if the library doesn't handle it.

null例如是 javascript 中的一个对象,因此如果库不处理它,您可能希望将其更改为空字符串。