I am recently trying to upgrade my ember from ember-cli: 2.6.2
to ember-cli: 3.17
and I am having a hard time trying to parallel my implementation of ajax service to the newer ember-cli version. I wasn't the one who created the app from the scratch that is why I do not know why the import { raw } from 'ic-ajax';
is needed. Any help are very much appreciated.
Error that I am getting: Uncaught (in promise) TypeError: Cannot read property 'status' of undefined
This is my old ajax service app/services/ajax.js
import Ember from 'ember';
import { raw } from 'ic-ajax';
import dasherizeObjectKeys from '../../utils/dasherize-object-keys';
const { inject, get, isEmpty } = Ember;
export default Ember.Service.extend({
currentSession: inject.service(),
request(url, method = 'GET', data = undefined, dataType = 'json') {
data = JSON.stringify(dasherizeObjectKeys(data));
return new Ember.RSVP.Promise((resolve, reject) => {
const contentType = 'application/json';
const token = this.get('currentSession.token');
const headers= {
'Authorization': `Token ${token}`,
'X-Client-Platform': 'Web'
};
raw({ url, headers, data, type: method, dataType, contentType })
.then((response) => this.handleSuccess(response, resolve))
.catch((response) => this.handleError(response, reject));
});
},
handleSuccess(response, resolve) {
return resolve({
status: response.jqXHR.status,
response: response.response
});
},
handleError(response, reject) {
if (response.jqXHR.status === 401 && this.get('currentSession.isAuthenticated')) {
this.get('currentSession').invalidate();
}
let error = get(response, 'jqXHR.responseJSON');
if (isEmpty(error)) {
const responseText = get(response, 'jqXHR.responseText');
// FIXME: server should always return json, empty string is not json,
// after backend is fixed, `JSON.parse` and try/catch for errors are not needed
try {
error = JSON.parse(responseText);
} catch (e) {
error = {
errors: { detail: responseText }
};
}
}
error.status = response.jqXHR.status;
reject(error);
}
});
And this is my new ajax service app/services/request.js
import Service from '@ember/service';
import { Promise } from 'rsvp';
import fetch from 'fetch';
import dasherizeObjectKeys from '../../utils/dasherize-object-keys';
export default class RequestService extends Service {
request(url, method = 'GET', data = undefined, dataType = 'json') {
data = JSON.stringify(dasherizeObjectKeys(data));
return new Promise((resolve, reject) => {
const contentType = 'application/json';
const token = this.get('currentSession.token');
const headers= {
'Authorization': `Token ${token}`,
'X-Client-Platform': 'Web'
};
fetch({ url, headers, data, type: method, dataType, contentType })
.then((raw) => this.handleSuccess(raw, resolve))
.catch((raw) => this.handleError(raw, reject));
})
}
handleSuccess(response, resolve) {
return resolve({
status: response.jqXHR.status,
response: response.response
});
}
handleError(response, reject) {
if (response.jqXHR.status === 401 && this.get('currentSession.isAuthenticated')) {
this.get('currentSession').invalidate();
}
let error = get(response, 'jqXHR.responseJSON');
if (isEmpty(error)) {
const responseText = get(response, 'jqXHR.responseText');
// FIXME: server should always return json, empty string is not json,
// after backend is fixed, `JSON.parse` and try/catch for errors are not needed
try {
error = JSON.parse(responseText);
} catch (e) {
error = {
errors: { detail: responseText }
};
}
}
error.status = response.jqXHR.status;
reject(error);
}
}
Aucun commentaire:
Enregistrer un commentaire