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

error.response.data is being a string when the data is JSON #797

Closed
MicroDroid opened this issue Mar 27, 2017 · 7 comments
Closed

error.response.data is being a string when the data is JSON #797

MicroDroid opened this issue Mar 27, 2017 · 7 comments

Comments

@MicroDroid
Copy link

MicroDroid commented Mar 27, 2017

Identical to #378

I'm using Axios version 0.15.3.

After my function failing, I added a console.log:

function extractError(error) {
	var result;
	console.log(error.response.data);
	if (error.response) {
		if (error.response.data.error)
			result = error.response.data.error + "!";
		else if (error.response.data.errors)
			result = error.response.data.errors.map(error => error.title).join(', ');
		else
			result = "Internal error!";
	} else {
		result = "Network error!";
	}
	return result;
}

and in the console, I'm getting this:

image

Which is purely a string and not an object, as accessing any of the field results in undefined

oh, and the function is used like:

// do something with axios
.catch(error => {
    this.message = extractError(error);
})
@dima74
Copy link

dima74 commented Dec 15, 2017

It happens for me only if response is not valid json. So I advice you recheck your server response.

@ariellevitsky
Copy link

You are likely getting a string response because you're converting the error into a string type by concatenating it with the "!".

@cubiczx
Copy link

cubiczx commented May 18, 2018

On Axios 0.18 say Uncaught (in promise) TypeError: error.response.data.errors.map is not a function

@nadim
Copy link

nadim commented Jun 7, 2018

I'm seeing it as well, and am not modifying the response in the catch block.

      console.log(error.response.data)
      console.log(typeof(error.response.data))

{ status: 'ASDF'}
string

From response headers: content-type: application/json;charset=UTF-8

From package.json: axios@0.18.0

@MicroDroid
Copy link
Author

I actually somehow go around that like a few days ago of posting this issue. I feel extremely guilty for not posting the solution here. In fact, I don't remember what I have done to make it work. 🤷‍♂️

@nadim
Copy link

nadim commented Jun 7, 2018

Is it for certain status codes only? It's happening on a 401 error for me, where I would like to parse the response.

@chinesedfan
Copy link
Collaborator

Seems @MicroDroid has solved his problem. For others, feel free to open a new issue if it is still there.

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants