首页 > 解决方案 > 等待辅助函数响应

问题描述

好的,所以我的帐户实体有一个控制器,它具有以下辅助功能

// Verify user accessing is account owner
const verifyOwnership = (account_id, owner_id) => {
  Account.findById(account_id, function (err, account) {
    if (err) return next(err);
    if (account.owner_id != owner_id) {
      return false;
    }
    console.log("here");
    return true;
  });
};

而我的控制器的账户更新功能如下

exports.account_update = function (req, res, next) {
  user_id = jwt.decode(req.headers["authorization"].slice(7)).id;
  account_id = req.params.id;

  // Verify user accessing is account owner
  ownership_verified = verifyOwnership(req.params.id, user_id);
  if (!ownership_verified) {
    res.status(403).send({ error: "You are not allowed to access this account." });
    return;
  }
  console.log(ownership_verified);

  // Update account
  Account.findByIdAndUpdate(account_id, { $set: req.body }, { new: true }, function (
    err,
    pet
  ) {
    if (err) return next(err);
    res.send({ message: "Account updated successfully.", account });
  });
};

现在,我对这段代码有几个问题:

抱歉发了这么长的帖子,并在此先感谢。

标签: javascriptnode.jsmongodbmongoose

解决方案


推荐阅读