Ember promise says failure when REST returned HttpStatus.OK..?

Hi guys,

I continue with learning of ember JS and I’m testing now ember data with UPDATING entities via simple REST API. My problem is simple, I’ve got the following action which invokes my REST PUT method. My commodity is saved successfully via hibernate, server returns HttpStatus.OK, but Ember promise ALWAYS calls failure function…???

In the reason I don’t see any error, just my HttpStatus.OK and ready state = 4. Which is very confusing.

actions: {
    save: function() {
      var commodity = this.modelFor('commodity');
      var post = this.store.update('commodity', commodity);

      var self = this;

      function transitionToPost(post) {
        alert('Commodity updated successfully');
        self.transitionToRoute('commodity');
      }

      function failure(reason) {
        alert(reason);
      } 

      post.save().then(transitionToPost).catch(failure);
    }
  }

I want to be able to catch specific HttpStatus codes from REST calls, do I have any chance with this in Ember data? Or do I need to implement this in the jquery?

thanks in advance

Tomas

It’s likely ember-data is throwing an exception which is triggering the error hook of the RSVP. In your failure callback console.error(reason.stack) to see the exception.

Hi jason,

yes, after some debugging I figured out that Ember.RSVP.Promise ends with failure “Unexpected end of input”.

Sended JSON to REST controller is:

{
    "commodity": {
        "name": "ATI-Radeon",
        "price": 500056565656,
        "count": 78778785454545,
        "itemshot": "http://community.amd.com/servlet/JiveServlet/downloadImage/38-2405-1795/RAMBannerWhite_980_text.jpg"
    }
}

which is valid JSON. Btw it seems that I’m gonna need to rewrite Ember Serializer, because by default it strips out the ID, which is very strange default behaviour.

To the problem, have you seen such error before? Most of the links suggests that the JSON is invalid, but that’s not my problem here…

thanks in advance

T.

Edit: I’m obviously missing something here, because ember invokes my spring controller with JSON entered in the page perfectly, data are saved, why Ember.RSVP.Promise crashes eventually at “parseerror” doesn’t make much sense.

What role plays Ember.RSVP.Promise during save exactly?

Ember-data creates a PromiseObject immediately returns that value and also passes it to the adapter to later resolve with the model that comes back https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/model/model.js#L978

The parse error is coming from your ember-data serializer. Your JSON being returned by your REST API is not in the JSON convention that ember-data expects (http://jsonapi.org/). If your API doesn’t adhere to this, you’ll need to normalize the payload on the normalizePayload (http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_normalizePayload) hook of the serializer.

There is plenty of documentation around this, so I won’t rehash it here.

Hi Jason,

thanks a lot for reply. Problem is solved now.

I didn’t noticed that Ember in cooperation with JQuery sets datatype attribute of Ajax call to dataType: ‘json’, which means that jquery expects that server will return a JSON presentation. Problem was that my Spring controller was returning a void, so after I changed my update method to return the input JSON from @RequestBody for example, everything started to work like a charm…

Thanks again for help, as far as my testing of REST calls with ember concerns, I’m going to just test Ember serializer to include ID of the Ember object to be sending to REST controller…By default ID of the object is striped out.

cheers

Tomas

I’m going to just test Ember serializer to include ID of the Ember object to be sending to REST controller…By default ID of the object is striped out.

From the DS.Serializer documentation:

NOTE: You may not want to include the ID when updating an existing record, because your server will likely disallow changing an ID after it is created, and the PUT request itself will include the record’s identification.

Ahhh, ok…:slight_smile: Yeah, so far I was taking ID of the updating record from the Spring’s @PathVariable, well ok, I can live with that…:slight_smile: