Can someone please provide information about the time when it comes up in the node.js Express application to cause this error:
throw new Error('my error');
or pass this error using a callback, usually labeled as follows:
next(error);
and could you please explain what each of them will do in the context of the Express application?
for example, here is an express function associated with URL parameters:
app.param('lineup_id', function (req, res, next, lineup_id) { // typically we might sanity check that user_id is of the right format if (lineup_id == null) { console.log('null lineup_id'); req.lineup = null; return next(new Error("lineup_id is null")); } var user_id = app.getMainUser()._id; var Lineup = app.mongooseModels.LineupModel.getNewLineup(app.system_db(), user_id); Lineup.findById(lineup_id, function (err, lineup) { if (err) { return next(err); } if (!lineup) { console.log('no lineup matched'); return next(new Error("no lineup matched")); } req.lineup = lineup; return next(); }); });
The line commented "// should I create my own error here?" I could use "throw new Error (" xyz ")", but what exactly is it to do? Why is it usually better to pass the error to the "next" callback?
Another question: how do I get "throw new Error (" xyz ") to display in the console as well as in the browser when I'm in development?
source share