When trying to parse a list of customers, the execution stops when fetching the json-response. I checked my API with Postman and all the data is returned as expected. I use the following code for authenticating with JWT
export default (type, params) => {
if (type === AUTH_LOGIN) {
const { username, password } = params;
const request = new Request('http://localhost:4000/api/auth/login', {
method: 'POST',
body: JSON.stringify({ username, password }),
headers: new Headers({ 'Content-Type': 'application/json' }),
});
return fetch(request)
.then(response => {
if (response.status < 200 || response.status >= 300) {
throw new Error(response.statusText);
}
return response.json();
})
.then(({ user }) => {
localStorage.setItem('token', user.jwt);
});
I use the authentication in the simplerest-client and I get the correct response.
const httpClient = (url, options = {}) => {
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' });
}
const token = localStorage.getItem('token');
options.headers.set('Authorization', `Token ${token}`);
return fetchUtils.fetchJson(url, options);
}
const restClient = simpleRestClient('http://localhost:4000/api', httpClient);
export default (type, resource, params) =>
new Promise(resolve => setTimeout(() => resolve(restClient(type, resource, params)), 500));
I added the content-range header (content-range →index 0-24/2) and my JSON-Response is:
{
"customers": [
{
"phone": "+1 39939311",
"last_name": "Test",
"join_date": "2017-05-17",
"is_active": false,
"id": 1,
"first_name": "Ein",
"email": "test@bla.com",
"birth_date": "1990-04-01",
},
{
"phone": "+44 88382832",
"last_name": "Fünf",
"join_date": "2017-01-01",
"is_active": false,
"id": 2,
"first_name": "Testen",
"email": "check@tester.org",
"birth_date": "1987-06-02",
}
]}
I get the following error message:
uncaught at handleFetch TypeError: newRecords.map is not a function
Does anybody have an idea, what I am doing wrong?