簡體   English   中英

如果在mongo集合中找不到ID,如何將響應發送給客戶端?

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

當用戶點擊應用程序但不屬於用戶管理集合時,我正在進行用戶身份驗證,我想將用戶ID發送給客戶端,以便他可以請求訪問權限。 我們正在使用csp cookie獲取用戶信息。

因此在下面的代碼中,如果User架構找不到User ID,我想將該信息發送給客戶端,以便用戶可以在請求訪問表單中使用。下面的代碼在架構找不到用戶ID時拋出404。 任何想法如何解決這個問題?

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));
};

如果沒有匹配項,則findById()返回[]

嘗試:

 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);
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM