如何调试失败的Ember Data JSON API save()操作?

时间:2015-10-07 07:41:33

标签: ember.js ember-data

我在路线中使用此操作创建了一个Ember Data对象:

    createProfile: function() {
        var profile = this.store.createRecord('profile', {
            username: 'bob',
            orientation: 'straight',
            gender: 'male',
            firstname: 'kevin',
            lastlogin: 'today'
        });
        profile.save().then(function() {
            this.transitionTo('welcome');
        });
    }

在服务器端,我处理POST请求并返回如下所示的响应:

Request URL: http://0.0.0.0/api/1/profiles
Request Method: POST
Status Code: 201 Created  <-- It has got the "Green Dot" in Ember Inspector

Response headers:
Access-Control-Allow-Headers: x-requested-with, Content-Type, origin, authorization, accept, client-security-token
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 1000
Connection: Keep-Alive
Content-Length: 164
Content-Type: application/vnd.api+json; charset=UTF-8
Date: Wed, 07 Oct 2015 07:17:06 GMT
Keep-Alive: timeout=5, max=100
Location: http://localhost/api/1/profiles/2
Server: waitress
access-control-allow-credentials: true

响应的主体看起来像这样:

{'data': {'type': 'profiles', 'attributes': {'firstname': 'kevin', 'lastlogin': 'today', 'username': 'bob', 'orientation': 'straight', 'gender': 'male'}, 'id': '2'}}

据我所知,这一切看起来都不错。我已经完成了另一个在此模型类型上成功执行GET方法的操作。我可以看到模型出现在Ember Data面板下的Ember Inspector中,看到它被正确地给出Id为2。

但是当服务器返回响应时,错误消息是:

createProfile/<@http://localhost/assets/emberpd.js:324:6
tryCatch@http://localhost/assets/vendor.js:60951:14
invokeCallback@http://localhost/assets/vendor.js:60966:15
publish@http://localhost/assets/vendor.js:60934:9
@http://localhost/assets/vendor.js:40181:7
Queue.prototype.invoke@http://localhost/assets/vendor.js:11532:9
Queue.prototype.flush@http://localhost/assets/vendor.js:11596:11
DeferredActionQueues.prototype.flush@http://localhost/assets/vendor.js:11392:11
Backburner.prototype.end@http://localhost/assets/vendor.js:10720:9
Backburner.prototype.run@http://localhost/assets/vendor.js:10842:13
run@http://localhost/assets/vendor.js:29679:12
ember$data$lib$adapters$rest$adapter$$RESTAdapter<.ajax/</hash.success@http://localhost/assets/vendor.js:66323:15
jQuery.Callbacks/fire@http://localhost/assets/vendor.js:3350:10
jQuery.Callbacks/self.fireWith@http://localhost/assets/vendor.js:3462:7
done@http://localhost/assets/vendor.js:9516:5
.send/callback@http://localhost/assets/vendor.js:9920:8

此时我觉得很困难。我使用Ember 2.0.2和Ember Data 2.0.1。

知道是否有一些明显的东西我不知道?

我在.then()函数中做错了吗?或者这是一个Ember数据问题?我知道我开始调试那种错误吗?

2 个答案:

答案 0 :(得分:1)

您承诺中的this与您期望的this的范围不同。您可以按如下方式重写它(在处理承诺之前将_this设置为this):

createProfile: function() {
  var profile = this.store.createRecord('profile', {
    username: 'bob',
    orientation: 'straight',
    gender: 'male',
    firstname: 'kevin',
    lastlogin: 'today'
  });

  var _this = this;
  profile.save().then(function() {
    _this.transitionTo('welcome');
  });
}

在ES6中,您可以重写此内容以避免必须设置_this

createProfile() {
  const profile = this.store.createRecord('profile', {
    username: 'bob',
    orientation: 'straight',
    gender: 'male',
    firstname: 'kevin',
    lastlogin: 'today'
  });

  profile.save().then(() => {
    this.transitionTo('welcome');
  });
}

答案 1 :(得分:0)

我建议它无法解析响应POST请求的有效负载。它是否与GET请求的结构相同?

无论如何,如果我是你,我会在handleResponse方法中设置断点并逐步解析有效负载以找出错误。