Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Object not getting returned in response #62

Open
abax1 opened this issue Jan 28, 2019 · 2 comments
Open

JSON Object not getting returned in response #62

abax1 opened this issue Jan 28, 2019 · 2 comments

Comments

@abax1
Copy link

abax1 commented Jan 28, 2019

I'm trying to return a json object but there is no response data coming back. It works fine over postman but not when using axios. Can someone help me with this?

My frontend code looks like this:-

export const getCollaborator = (collaboratorID, dispatch) => {
  axios
    .get(`http://localhost:8081/collaborators/${collaboratorID}`,
    {
      headers:{
        "Accept": "application/json"
      }
    })
    .then(
      // success
      ({ data, status }) => {
        console.log("status", status);
        
        return dispatch({
          type: GET_COLLABORATOR,
          payload: data
        });
      }
    ).catch(error => {
      if (error.response) {
        // The request was made and the server responded with a status code
        // that falls out of the range of 2xx
        console.log(error.response.data);
        console.log(error.response.status);
        console.log(error.response.headers);
      } else if (error.request) {
        // The request was made but no response was received
        // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
        // http.ClientRequest in node.js
        console.log(error.request);
      } else {
        // Something happened in setting up the request that triggered an Error
        console.log('Error', error.message);
      }
      console.log('====================================');
      console.log(error);
      console.log('====================================');
      console.log(error.config);
    });
};

And the mock file:-

HTTP/1.1 200 OK
Content-Type: application/json

#import './data.json';

data.json:-

{
  "collaborator_id": 1,
  "username": "[email protected]",
  "profile_desc": "I am amazing",
  "primary_expertise": 1,
  "secondary_expertise": 3,
  "primary_other": "",
  "secondary_other": "",
  "weblinks": [
    {
      "type": "personal",
      "url": "www.my-site.com"
    },
    {
      "type": "linkedIn",
      "url": "www.linkedIn.com"
    }
  ],
  "skills": [
    {
      "skill_name": "java",
      "skill_level": 1
    },
    {
      "skill_name": "React",
      "skill_level": 2
    },
    {
      "skill_name": "Python",
      "skill_level": 3
    }
  ],
  "education": [
    {
      "school_name": "Cambridge University",
      "country": 1,
      "qualification": "B. Sc.",
      "major": "1st Class",
      "year": 1999
    },
    {
      "school_name": "Oxford University",
      "country": 1,
      "qualification": "B. Sc.",
      "major": "1st Class",
      "year": 2000
    }
  ]
}
@odino
Copy link
Contributor

odino commented Jan 29, 2019

Log of the response / error with axios? :)

@abax1
Copy link
Author

abax1 commented Jan 29, 2019

I'll try and reproduce it and send the error log. I have tried the same call using json-server and it works fine. Very odd. Not sure what I am doing wrong. Would be grateful if someone could try and reproduce in case it is my environment that is causing an issue.

It was returning Network Error with no status code. It goes straight here:-

.catch(error => {
      if (error.response) {
        // The request was made and the server responded with a status code
        // that falls out of the range of 2xx
        console.log(error.response.data);
        console.log(error.response.status);
        console.log(error.response.headers);
      } else if (error.request) {
        // The request was made but no response was received
        // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
        // http.ClientRequest in node.js
        console.log(error.request);
      } else {
        // Something happened in setting up the request that triggered an Error
        console.log('Error', error.message);/* <-- code executes here */
      }
      console.log('====================================');
      console.log(error);
      console.log('====================================');
      console.log(error.config);
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants