s_curry_s
s_curry_s

Reputation: 3432

Backbone Create

For some reason this.collection.create() is returning an error, but interestingly, the error message seems like the model I just added to my collection.

this.collection.create({
    name: $('#big-input').val(),
    firstRemark: $('#small-input').val(),
    postedBy: window.user.displayName,
    twitterHandle: window.user.twittername,
    pictureUrl: window.user.profilePic

},{wait: true,    

  success: function(response){
     console.log("success");
     console.log(response)

  },
  error:function(err){
      console.log(err)

   }

});

this is what I get after console.log(err):

enter image description here

exports.newPost = function(req, res){
  console.log("GOT NEW TWEET REQUEST");
  var newPost = new Post(req.body)
  newPost.dateCreated = new Date();
  newPost.save();
  res.send(200);
};

Thanks to answers below I was able to print my 'real' error. As seen below xhr.responseText is 'OK' and 'status' is 200. Why is this response triggering a success but an error?

enter image description here

I also have a parse method in my collection

parse: function(response){
    this.page = response.page;
    this.perPage = response.perPage;
    this.total = response.total;
    this.noofpages =response.noofpages;

    return response.posts;
},

Upvotes: 2

Views: 127

Answers (2)

obmarg
obmarg

Reputation: 9559

This is expected. Have a look at the Model.Save documentation, which says the error callback will be called with (model, xhr, options) as it's parameters.

If you want the actual contents of the response you can get it from the second parameters responseText property: xhr.responseText. There's more details on jqXHR elements in the jquery documentation: http://api.jquery.com/jQuery.ajax/#jqXHR

The parameters of your success callback are also not quite right - it takes (model, response, options)

EDIT:

Not entirely sure if it's the cause of your problems, but your server should be returning a 200 status code and the models JSON on success. Have a look at the Backbone.Sync documentation.

From looking at the code this does look important when passing wait: true as an option, as the attributes set are extended with the returned attributes from the server. Have a look at the options.success function used by backbone here to see what I mean. It certainly looks like something might go wrong if you return "OK" from the server, though I'm not sure if it'd be exactly the problem you're experiencing.

EDIT2: Slight correction to what I wrote above: The return value gets passed through Model.parse. Since you've defined a custom parse function, the server should return something that is going to work with it, rather than just plain JSON for the model.

Upvotes: 3

Lee
Lee

Reputation: 10603

The error function will receive 3 arguments as defined in the wrapError function in backbone.js

  model.trigger('error', model, resp, options);

Therefore what your output is, is correct. You will want to add a variable to your error function to capture the response (2nd argument) and log that instead to help you debug.

Upvotes: 3

Related Questions