2017-05-30 117 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。你知道嗎,這可能是罪魁禍首嗎?再一次,任何幫助非常感謝,夥計們。謝謝 –