Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Success callback never triggered with Ember-Data save()

I am trying to use ember-data to get a simple registration form to save on my server. The call technically works, but the success callback is never trigger on the promise, and I have no idea why.

The server receives the data from the front end and successfully saves it to the database. It then returns status code 201 for CREATED. I can see the successful response happening in the Chrome debugger. But even when the server responds with a successful status, the error callback is triggered on the save's promise. I've confirmed this happens every time by putting a debugger; statement in the error callback.

My router's model is hooked up like this:

model: function() {
  return this.store.createRecord('registerUser');
}

And I have a simple register function in my controller:

register: function() {
  var self = this;
  this.get('model').save().then(function() {
     self.transitionToRoute('index');
  }, function(resp) {
     if (resp.responseJSON) {
       self.get('model').set('errors', resp.responseJSON.errors);
     } 
  });
}

Every time my server comes back with a response, success or failure, the failure callback is hit. If I have errors in the response (for invalid data or something), the errors are successfully displayed in the form. I can see the request coming in properly, and the data is stored in the database. So, the save is technically successful, but ember doesn't seem to know that it is even though a successful 201 status is returned from the server (which can be verified in the Chrome debugger).

The only thing I can think of is that ember-data's adapter is doing something that I'm not aware of, but I am just using the default RESTAdapter and haven't touched it. Is there anything else

If it makes a difference, the server is running Play 1.2.5. I don't know if that makes a difference in the response's header or something like that.

Any help would be greatly appreciated. Thank you for your time!

Mike

SOLUTION

So, the issue was to do with the JSON response. The two problems:

  • I did not include an ID in the response
  • I did not "wrap" the response in a "registerUser". This is necessary to match the model name.

Below is a valid response:

{
  "registerUser": {
    "id": 11,
    "email": "[email protected]",
    "password": "12345",
    "password2": "12345",
    "name": "Mike"
  }
}
like image 842
Michael O Avatar asked Jan 30 '14 01:01

Michael O


1 Answers

Ember Data is expecting the model in the response, so sending back a success http status doesn't mean it will hit the success endpoint. When it tries to serialize your response (or lack of response) it's probably failing which would be why it's hitting the failure function. A big reason for the response is the id of the record.

The model returned should be in the following format

{
   registerUser:{
     id: "123",
     attr: "asdf"
   }
}

https://github.com/emberjs/data/blob/master/TRANSITION.md

like image 129
Kingpin2k Avatar answered Nov 25 '22 01:11

Kingpin2k