2

I am trying to request an API without ajax call (why would I import the whole library for simply making an ajax call?).

So I decided to follow the fetch function flow as it is described in the Redux doc (which I migrate with last week).

import fetch from 'isomorphic-fetch'

Thus, I have this short function:

export function fetchPosts(value) {
    return dispatch => {
        dispatch(requestPosts(value))
        return fetch(Config.serverUrl + '/' + value, {
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                Authorization: Config.authorizationToken
            },
        })
          .then(response => response.json())
          .then(json => dispatch(receivePosts(value, json)))
    };
};

but when I do a console.log in this next step function:

function receivePosts(value, json) {
    console.log(json);
    return {
        type: RECEIVE_POSTS,
        value,
        posts: json.data.children.map(child => child.data)
    };
};

I can see that my json is an empty object.

The network return this:

General: Request URL:https://shelob-v2.dev.blippar.com/v2/categories Request Method:OPTIONS Status Code:200 OK Remote Address:52.25.79.166:443

Header: Access-Control-Allow-Headers:Authorization, Content-Type, X-Pouet-UniqueID, X-Pouet-UniqueRunID Access-Control-Allow-Methods:PATCH, PUT, DELETE Access-Control-Allow-Origin:* Content-Length:0 Content-Type:text/plain; charset=utf-8 Date:Wed, 27 Apr 2016 14:14:36 GMT

Am I doing something definitely wrong with it?

1
  • Can you look at the request/response in the network tab (chrome)? Commented Apr 27, 2016 at 14:13

1 Answer 1

2

Your redux syntax looks correct, but it looks like the request was an OPTIONS request and not a GET request. Try adding method: 'GET' to your fetch options.

export function fetchPosts(value) {
    return dispatch => {
        dispatch(requestPosts(value))
        return fetch(Config.serverUrl + '/' + value, {
            method: 'GET', // here
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                Authorization: Config.authorizationToken
            },
        })
          .then(response => response.json())
          .then(json => dispatch(receivePosts(value, json)))
    };
};
Sign up to request clarification or add additional context in comments.

2 Comments

Indeed, this time my json is filled ! Do you have any idea why I still have this output error: Cannot read property 'children' of undefined ? is the treatment on the json of receivePosts deprecated?
It sounds like this code is incorrect: json.data.children.map(child => child.data). I would use network dev tools or add a console.log(json) in your fetchPosts or receivePosts to know exactly what your response looks like and go from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.