简体   繁体   中英

How to send response to client if id could not find in mongo collection?

I am working on user authentication when user hit the application and is not part of user management collection i want to send user id to client so he can request access. we are getting user information using csp cookies.

So in below code if User schema could not find useruid i want to send that information to client so user can use in request access form.with below code its throwing 404 when schema don't find useruid. Any idea how to resolve this issue ?

user.controller.js

   function handleEntityNotFound(res) {
  return function(entity) {
    if (!entity) {
      res.status(404).end();
      return null;
    }
    return entity;
  };
}

    exports.current = function(req, res) {
      // console.log('username', req);
      User.findById(req.useruid)
          .populate({path: 'groups', select: '_id name', options: {sort: {name: 1}}})
      .execAsync()
    .then(handleEntityNotFound(res))
    .then(responseWithResult(res))
    .catch(handleError(res));
};

If there's no match, findById() returns [] .

Try:

 exports.current = function(req, res) {
        User.findById(req.useruid, function (err, results) {
            if (err) { 
                console.log(err);
            }
            if (results.length) {
                responseWithResult(res);
            } else {
                handleEntityNotFound(res);
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM