Reputation: 582
I'm trying a simple backbone.js application. And getting error on doing collections fetch and not able to figure out why. Any pointers to check for will be helpful.
1- My backend is java based.
2- here are my model and collection
var lesson = Backbone.Model.extend
({
defaults :
{
lessonId: '',
lessonName: '',
subject: '',
textBook: '',
},
initialize: function()
{}
});
var lessonsCollection = Backbone.Collection.extend
({
model: lesson,
url: "/lessons?subject=Math&class=5",
initialize : function(){
//var items = this.fetch();
//console.log(items);
}
});
3- The response produced from server on the data request is :
[
{"lessonId":"00000001","lessonName":"DemoFractions","subject":"Math","textBook":"Null"},
{"lessonId":"00000002","lessonName":"test ","subject":"Math","textBook":"test"}
]
4- My code to make fetch call looks like
var coll = new lessonsCollection();
coll.fetch(
{
success: function(){
console.log("got data");
} ,
error: function(){
console.log("error in getting data through rest");
}
}
);
However everytime the fetch call is resulting in error. What is it that I'm missing in my code?
EDITED: Based on suggestion I'm now logging the response. My code looks like
coll = new lessonsCollection;
coll.fetch(
{
success: function(){
console.log("got data");
self.render() ;
} ,
error: function(collection, response){
console.log("error in getting data through rest");
console.log("response text got ",response.responseText);
console.log(response);
}
}
);
The console log appears like a js code to me.
error in getting data through rest
response text got
({readyState:0, getResponseHeader:(function (e){var t;if(2===b){if(!c){c={};while(t=Tn.exec(a))c[t[1].toLowerCase()]=t[2]}t=c[e.toLowerCase()]}return null==t?null:t}), getAllResponseHeaders:(function (){return 2===b?a:null}), setRequestHeader:(function (e,t){var n=e.toLowerCase();return b||(e=v[n]=v[n]||e,y[e]=t),this}), overrideMimeType:(function (e){return b||(p.mimeType=e),this}), statusCode:(function (e){var t;if(e)if(2>b)for(t in e)m[t]=[m[t],e[t]];else C.always(e[C.status]);return this}), abort:(function (e){var t=e||w;return u&&u.abort(t),k(0,t),this}), state:(function (){return n}), always:(function (){return i.done(arguments).fail(arguments),this}), then:(function (){var e=arguments;return x.Deferred(function(n){x.each(t,function(t,o){var a=o[0],s=x.isFunction(e[t])&&e[t];i[o[1]](function(){var e=s&&s.apply(this,arguments);e&&x.isFunction(e.promise)?e.promise().done(n.resolve).fail(n.reject).progress(n.notify):na+"With"})}),e=null}).promise()}), promise:(function (e){return null!=e?x.extend(e,r):r}), pipe:(function (){var e=arguments;return x.Deferred(function(n){x.each(t,function(t,o){var a=o[0],s=x.isFunction(e[t])&&e[t];i[o[1]](function(){var e=s&&s.apply(this,arguments);e&&x.isFunction(e.promise)?e.promise().done(n.resolve).fail(n.reject).progress(n.notify):na+"With"})}),e=null}).promise()}), done:(function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}), fail:(function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}), progress:(function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}), complete:(function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}), success:(function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}), error:(function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}), responseText:"", status:0, statusText:"error"})
thanks
Upvotes: 0
Views: 187
Reputation: 146014
Well, the specific error you are getting back in the response would be really helpful for us to see, but you need at least to set idAttribute
on your Lesson
model.
var Lesson = Backbone.Model.extend({
idAttribute: 'lessonId',
defaults: {
lessonId: '',
lessonName: '',
subject: '',
textBook: ''
}
});
See if that fixes it. Otherwise, log the response:
error: function(collection, response){
console.log("error in getting data through rest", response);
}
Upvotes: 0