Backbone fetch () throws Uncaught TypeError: Cannot read ajax property from undefined

I am integrating BackBone in a new project. Although I am a little familiar with BackBone, this will be my first attempt to create a new project with it.

I set up the model and view, but I get a console error that does not return any helfpul results in StackOverflow or Google. I hope someone here can determine where I made a mistake.

Here's the error that occurs when I call thing.fetch()

 Uncaught TypeError: Cannot read property 'ajax' of undefined 

And here is my CoffeeScript code:

 Thing = Backbone.Model.extend name: 'thing' url: -> "/things/#{@id}" ThingView = Backbone.Model.extend el: "#thing" render: -> console.log 'render', @$el thing = new Thing(id: 1) thing.fetch() thingView = new ThingView()(model: Thing) thingView.render() 
+4
source share
2 answers

UPDATE: Solved this on my own. Newbie error, not loading first requests. I realized that before jQuery, and not after jQuery, I ran into and downloaded Backbone. The ajax method was an undefined property because jQuery did not exist at this point in my code.

Many times a simple solution is the right one. Thanks to everyone who jumped in to help!

+5
source

As the comments say, it looks like you are seeing this error because you are trying to call $ .ajax (via fetch) but $ is undefined (i.e. jQuery is not loaded correctly).

If you post more information, I can try and give more information / details on how to fix this problem.

A few other notes I wanted to make ...

  • ThingView = Backbone.Model.extend should be ThingView = Backbone.View.extend (view no model)

  • In coffeescript, I would write the class Thing extends Backbone.Model instead of Thing = Backbone.Model.extend and the same for ThingView

  • I'm also confused by your syntax here new ThingView()(model: Thing) looks like a single deuce, but maybe I'm forgetting something here

You don't need this here, as this is apparently just a jQuery problem, but you may need to debug future issues with the underlying applications using backbone.debug (disclaimer: I created this)

+2
source

All Articles