I'm trying to return the axios response in the function validateUser()
. In the code below, the console.log()
works but the return
doesn't. I've tried other questions' answers but nothing helped.
import axios from 'axios';
export async function validateUser(user) {
if (user.length < 1) return "This field is required";
else if (user.length > 24) return "Max 24 chars";
else checkUser(user).then(res => {console.log(res); return res});
}
export async function checkUser(user) {
try {
let res = await axios.post('http://127.0.0.1:8000/checkUser/', {
user : user,
})
if (res.data == 0) { console.log("0"); return true; }
else if (res.data == 1) { console.log("1"); return "This field is required"; }
else if (res.data == 2) { console.log("2"); return "User not available"; }
}
catch(err) {
console.log(err);
return "Unknown error";
}
}