简体   繁体   中英

“Undefined is not a function” error when calling a Backbone collection's fetch function

I am getting an error when calling the fetch function.

Undefined is not a function Error in Backbone js 

 var models = Backbone.Model.extend({ userId: function() { return this.get('userId'); }, id: function() { return this.get('id'); }, body: function() { return this.get('body'); } }); //Collections var todolistStore = Backbone.Collection.extend({ url: function() { return 'https://jsonplaceholder.typicode.com/posts' }, model: models, parse: function(response) { return response; } }); todolistStore.fetch(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js" type="text/javascript"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script> 

There are a couple of problems. I'm building off of @ChrisG 's answer.

  1. You need to instantiate a collection. Here's what my code:

     var Model = Backbone.Model.extend({ userId: function() { return this.get('userId'); }, id: function() { return this.get('id'); }, body: function() { return this.get('body'); } }); //Collections var TodoListStore = Backbone.Collection.extend({ url: function() { return 'https://jsonplaceholder.typicode.com/posts' }, model: Model, parse: function(response) { return response; } }); var todolistStore = new TodoListStore(); todolistStore.fetch(); 
  2. I updated the version of Underscore and Backbone

    https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM