madhums / node-express-mongoose-demo

A simple demo app using node and mongodb for beginners (with docker)

Home Page:https://nodejs-express-demo.fly.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error handling doesn't work on local environment?

Kaijun opened this issue · comments

for example i was requesting

/articles/somewrongid

the request kept pending, on console:

GET /articles/somewrongid - - ms - -

but i tried this on the heroku site,
it works totally fine.

what should be the difference?

seems that mongoose wont return if it doesn't find any matched id:

exports.load = wrap(function* (req, res, next, id) {
  req.article = yield Article.load(id);
  // stuck here!!!
  if (!req.article) return next(new Error('Article not found'));
  next();
});
exports.load = wrap(function* (req, res, next, id) {
  try{
    req.article = yield Article.load(id);
  }
  catch(e){
    console.log(e)
  }
  if (!req.article) return next(new Error('Article not found'));
  next();
});

It works if i add the error handling.
Must add the try catch statement so code won't stuck here and move on?

It seems to be a problem of findOne() method of Mongoose.

for me,

findOne({_id}) will throw an CastError instead of null, however it would return null if i'm querying with other fields e.g. title.

Yea, that looks like an issue with mongoose/mongodb. I have added some more error handling which should make it behave a little better now.