简体   繁体   中英

Ember: Promise (destroyRecord().then()) failing on successful request

I am very new to ember, but I've spent hours with this problem and can't solve it on my own. Here's my route (using ember-cli):

import Ember from 'ember';

export default Ember.Route.extend({
    actions: {
        save: function() {
            var controller = this.controller;
            controller.get('model').save().then(function(account) {
                console.log('account saved');
                controller.transitionToRoute('accounts.index');
            }, function(response) {
                console.log('account NOT saved');
            });

            return false;
        },
        deleteAccount: function() {
            var controller = this.controller;
            controller.get('model').destroyRecord().then(function(account) {
                console.log('account deleted');
                controller.transitionToRoute('accounts.index');
            }, function(response) {
                console.log('account NOT deleted');
            });

            return false;
        },
        cancel: function() {
            this.controller.get('model').rollback();
            this.transitionToRoute('accounts.index');
            return false;
        },
    }
});

I am triggering the deleteAccount action in my template (button). The interesting thing is that the code is actually deleting the record. It sends a successful delete request and the api deletes the account. But it never transitions to accounts.index. Instead it logs "account NOT deleted". If I manually go to account.index then the model isn't there any more (as one would expect). I got the code from the official ember docs. See: http://emberjs.com/api/data/classes/DS.Model.html#method_destroyRecord

So why is the promise always failing when the model is actually deleted? Your help would be very much appreciated!

Btw.: It is an edit route with account_id passed as param, so no need for manually defining a "model function" on the route. Just in case someone was wondering.

I guess I've just solved it. The reason for the failing of the destroyRecord() promise seemed to be that my API responded with an EMPTY HTTP 200 response. But 200 usually implies that an entity is returned which isn't the case. So I adapted the API to return an empty 204 response and this did the trick. This SO answer actually helped a lot: HTTP status code for update and delete?

A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has not yet been enacted, or 204 (No Content) if the action has been enacted but the response does not include an entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM