2017-05-30 114 views
1

尝试理解使用我的api和ember-cli-mirage之间的响应不一致性。从XHR对象中缺少ember-cli-mirage响应标头

我有一个处理程序等待POST请求的响应来验证用户。处理器的预期参数是responsestatusxhr

(例如.then(function(response, status, xhr) {...})。

使用我的API我收到我期望的结果 - 响应是数据,status是statusText,xhr是xhr对象。然而,使用ember-cli-mirage一切都在响应(类型)之下,而status和xhr都是未定义的。我的代码

片断低于:

海市蜃楼/ config.js

this.post(URI.AUTH_SIGN_IN, function(db, request) { 
    const responseHeaders = { 
    'access-token': 'abcxyz123', 
    'client': 'foobarbaz', 
    'token-type': 'Bearer', 
    'expiry': '1497364419', 
    'uid': '[email protected]' 
    }; 

    const user = { 
    data: { id: 1, type: 'user', attributes: { uid: '[email protected]', email: '[email protected]', name: 'John Doe', provider: 'email' } } 
    }; 

    return new Mirage.Response(200, responseHeaders, user); 
}); 

鉴别器/ devise.js

authenticate(identification, password) { 
    ... 
    this.makeRequest(credentials).then(function(response, status, xhr) { 
    // persists the five headers needed to send to devise-token-auth 
    // with mirage; response = Response {type: "default", status: 200, ok: true, statusText: "OK", headers: Headers…}, status = undefined, xhr = undefined 
    // with actual api; response = Object {data: Object}, status = "success", xhr = Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function…} 

    // As a result below will fail :( 
    // TypeError: Cannot read property 'getResponseHeader' of undefined 
    var result = { 
     'access-token': xhr.getResponseHeader('access-token'), 
     expiry:   xhr.getResponseHeader('expiry'), 
     tokenType:  xhr.getResponseHeader('token-type'), 
     uid:   xhr.getResponseHeader('uid'), 
     client:   xhr.getResponseHeader('client') 
     }; 
    }); 
} 

我相信我做所有正确的,但我知道是错误的:)。任何帮助深表感谢。

+0

这个@TomDoe的新闻? –

回答

0

嗯,我不确定为什么makeRequest返回未定义的第二和第三参数。

我做了一个简单的摆弄和args作为getJSON似乎正确:

https://ember-twiddle.com/70229e352f37b4e437ced8509a4415d9?openFiles=routes.application.js%2C

model() { 
    return Ember.$.getJSON('/foo').then((data, response, object) => { 
    return object.getAllResponseHeaders(); 
    }); 
} 

可能有一些与伪装者如何处理嘲笑响应或iwth如何makeRequest作品略有不同,所以我建议先看看那里。

+0

看起来像是'ember-fetch'的结果https://github.com/simplabs/ember-simple-auth/blob/1.3.0/addon/authenticators/devise.js#L202。你知道吗,这可能是罪魁祸首吗?再一次,任何帮助非常感谢,伙计们。谢谢 –