Answer the question
In order to leave comments, you need to log in
How to make error handling through axios last?
For example, I send a request to an address via axios axios.post('/api/users', {username: ''});
handler looks like this
let router = express.Router();
function validateInput(data) {
let errors = {};
if(Validator.isNull(data.username)) {
errors.username = 'This field is required';
}
return {
errors,
isValid: isEmpty(errors)
}
}
router.post('/', (req, res) => {
const {errors, isValid} = validateInput(req.body);
if(!isValid) {
res.status(400).json(errors);
}
});
export default router;
Answer the question
In order to leave comments, you need to log in
And why through Promise? You have the usual synchronous verification code and that's it. Let it be:
...
router.post('/', (req, res) => {
const {errors, isValid} = validateInput(req.body);
if(!isValid) {
return res.status(400).json(errors);
} else {
return res.status(200).json({data: 'bla-bla-bla'})
}
});
...
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question