Javascript 主干视图收集错误

Javascript 主干视图收集错误,javascript,node.js,backbone.js,express,Javascript,Node.js,Backbone.js,Express,我是个新手,我只是想让这种观点发挥作用。我想从用户填写的表单中输入一个集合数据,然后将用户重定向到其他地方。但是,我不断得到一个未定义的错误,即: Uncaught TypeError: undefined is not a function 以下是导致错误的代码: //this lives within a Backbone view, as does the following constructor. clickSubmitRegister: function(e) {

我是个新手,我只是想让这种观点发挥作用。我想从用户填写的表单中输入一个集合数据,然后将用户重定向到其他地方。但是,我不断得到一个未定义的错误,即:

 Uncaught TypeError: undefined is not a function 
以下是导致错误的代码:

 //this lives within a Backbone view, as does the following constructor.
 clickSubmitRegister: function(e) {
     e.preventDefault();
     var formData = {};
     $('#registrationForm').find('input').each(function() {
         value = $(this).val();
         thisId = $(this).attr('id');
         if (value != '') {
             formData[thisId] = value;
         }
     });
     console.log(formData); //this displays the correct data, all good
     this.collection.create(formData); //this line throws me the error
 },
初始化构造函数如下所示:

initialize: function() {
    this.collection = new app.User();
    this.render();
},

如果您通过这个.events对象正确设置了click事件,它应该可以像您预期的那样工作。并且您输入的代码没有任何问题。 因为如果通过主干事件设置事件,则始终引用视图实例

因此,只有两个可能的原因会导致错误

这不引用视图实例。这意味着您明确地更改了它引用的内容,或者使用了类似$this.el.onclick的内容,因为这会将它引用的内容更改为clicked元素

调用initialize后,this.collection以某种方式被重写。 ofc你有可能这个.collection.create也被替换了

我们无法同时检查它们,因为您尚未输入完整的代码

所以试试看

     //this lives within a Backbone view, as does the following constructor.
     clickSubmitRegister: function(e) {
         e.preventDefault();
         var formData = {};
         $('#registrationForm').find('input').each(function() {
             value = $(this).val();
             thisId = $(this).attr('id');
             if (value != '') {
                 formData[thisId] = value;
             }
         });
         console.log(formData); //this displays the correct data, all good
         console.log(this); //to check which object 'this' refers
         console.log(this.collection); //to check if this.collection got replaced or not
     },

clickSubmitRegister通过包含在events对象中的click事件调用,那里一切正常,this.collection应该引用this.collections,构造函数中的collections应该引用collections用户,该用户位于collections文件夹中的一个文件中。控制台上有很好的建议。logthis,完全忘了运行它。刚刚意识到这与不正确的模型语法有关。