Node.js
Restful API Design: Best Practices
Zoeken…
Foutafhandeling: haal alle bronnen op
Hoe gaat u om met fouten en logt u ze niet in op de console?
Slechte manier:
Router.route('/')
.get((req, res) => {
Request.find((err, r) => {
if(err){
console.log(err)
} else {
res.json(r)
}
})
})
.post((req, res) => {
const request = new Request({
type: req.body.type,
info: req.body.info
});
request.info.user = req.user._id;
console.log("ABOUT TO SAVE REQUEST", request);
request.save((err, r) => {
if (err) {
res.json({ message: 'there was an error saving your r' });
} else {
res.json(r);
}
});
});
Beter:
Router.route('/')
.get((req, res) => {
Request.find((err, r) => {
if(err){
console.log(err)
} else {
return next(err)
}
})
})
.post((req, res) => {
const request = new Request({
type: req.body.type,
info: req.body.info
});
request.info.user = req.user._id;
console.log("ABOUT TO SAVE REQUEST", request);
request.save((err, r) => {
if (err) {
return next(err)
} else {
res.json(r);
}
});
});
Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow