Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnhandledPromiseRejectionWarning error on mongoose.remove/router.delete #49

Open
charles-passille-smartnets opened this issue Feb 24, 2017 · 0 comments

Comments

@charles-passille-smartnets

Not sure if same error as #47 but...

I'm doing deepPopulation of comments in posts, trying to delete comments via ajax delete call.
This error occurs on pages that uses the deep populate.

(node:16600) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): CastError: Cast to ObjectId failed for value "null" at path "_id" for model "Post"
events.js:160
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read property 'commentsIds' of undefined
    at \routes.js:155:24
    at \node_modules\mongoose-deep-populate\lib\plugin.js:70:33
    at \node_modules\mongoose\lib\query.js:2559:9
    at process._tickCallback (internal/process/next_tick.js:103:7)


Routes.js:155 looks like this

router.get('/post/:id', function(req, res) {
  Post.findById(req.params.id).deepPopulate('author commentsIds commentsIds.userId').exec(function(err, post){
      res.render('singlepost', {
        user: req.user,
        title : "post",
        post : post,
        comments : post.commentsIds,   <-- 155
        page : 'singlepost'
      });
    });
});

the delete call looks like this

router.delete('/remove', function(req, res, next){
    Comments.findOneAndRemove({ _id: req.body.comment }, function(err) {
      if (err){
        console.log(err);
      }
      else{
        res.status(200).send({success: true});
      }
    });
});

I know req.body.comment is properly passing through.

The error occurs on the get, like it's trying to reload the page, but it shouldn't as i'm making an ajax post call and event.preventdefault + javascript:void(0) on the tag are properly working.

Any idea? I'm really lost now.

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant