【问题标题】:Ember-data.js 500 status codeEmber-data.js 500 状态码
【发布时间】:2015-02-23 06:42:01
【问题描述】:

在创建新模型时有什么方法可以处理 500 状态错误,例如

var model = this.store.createRecord('user');
model.save().then(function() {

}, function(error) {
// this callback will not be executed if 500 status response
});

但是,我可以使用模型上的becameError 事件来捕获它,但在这种情况下,错误对象将不可用。

感谢您的回答。

【问题讨论】:

  • 我不知道为什么 500 不会回到拒绝钩子,也许我需要创建一个快速模拟来查看它。没有中钩吗?
  • 您找到解决方案了吗?我遇到了同样的问题。我也尝试过像这样使用 catch:javascript var model = this.store.createRecord('user'); model.save().then(function() { }).catch(function(error) { // this callback will not be executed if 500 status response });
  • 我发现了我的问题。我在适配器中覆盖了“createMethod”,并且在那里捕获了不允许它们传播到控制器的捕获的错误。

标签: ember.js ember-data


【解决方案1】:

看起来 500 让它回到了拒绝路线

http://emberjs.jsbin.com/OxIDiVU/159/edit

【讨论】:

  • kingpin2k,感谢您的回答!但我认为我会处理一些罕见的情况)我会尝试在 jsbin 中重现它。
【解决方案2】:

您可以覆盖 DS.Model 保存并将错误哈希中的值分配给您的模型

App.Model = DS.Model.extend({
  save: function() {
    var _this = this;
    return this._super().then(function(obj) {
      return new Ember.RSVP.Promise(function(resolve) {
        return resolve(obj);
      });
    }, function(error) {
      //Do something with error here
      _this.set('error_status', error.status)
      return new Ember.RSVP.Promise(function(resolve, reject) {
        return reject(error);
      });
    });
  }
});

注意。在 save 方法中的 error 函数之前调用了 becomeError 事件,因此在调用 becomeError 时没有设置 'error_status'。

【讨论】:

    【解决方案3】:
    var model = this.store.createRecord('user');
    model.save().catch(function(error) {
      error.status; // status code
    });
    

    【讨论】:

      猜你喜欢
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 2017-03-18
      相关资源
      最近更新 更多