My postmapping “login” in my controller is returning a 200. But I’m getting undefined and I believe it is from my axios call. I know that the undefined error is being reported from the catch block in the console
Axios call –
JavaScript
x
23
23
1
submit() {
2
let formData = new FormData();
3
formData.set("email", this.email)
4
formData.set("password", this.password)
5
formData.set("staySignedIn", this.staySignedIn)
6
7
// When the client/server sides are running from the same port (AWS) the url for this api call should be changed to /api/v1/login
8
axios.post("http://localhost:8080/api/v1/login", formData,
9
{headers: {'Content-Type': 'application/json'}})
10
.then(function (res) {
11
console.log(res); // test
12
if (res.data.code === 200) {
13
this.router.push('/dashboard')
14
console.log("success");
15
} else {
16
console.log(res.data.code);
17
}
18
})
19
.catch(function (err) {
20
console.log(err);
21
})
22
}
23
Advertisement
Answer
Axios response schema documentation is here
Unless you have a key code
in your controller response, response.data.code
will be undefined.
Try res.status
instead if you want to check the HTTP status.
JavaScript
1
14
14
1
axios.post("http://localhost:8080/api/v1/login", formData,
2
{headers: {'Content-Type': 'application/json'}})
3
.then(function (res) {
4
if (res.status === 200) {
5
this.router.push('/dashboard')
6
console.log("success");
7
} else {
8
console.log(res.status);
9
}
10
})
11
.catch(function (err) {
12
console.log(err);
13
})
14
EDIT You seem to be sending back the password back in the response. Even though the password is encrypted, better restrict exposing it in the response.