【问题标题】:500 Internal Server Error: cannot update user in mongoose500 内部服务器错误:无法更新猫鼬中的用户
【发布时间】:2016-06-04 14:36:17
【问题描述】:

我正在尝试运行 put 函数来更新我的 mongoDB 中的用户,但我收到 500 内部服务错误。我正在使用angular-fullstack 生成器

我在我的控制器中使用这个资源:

  update(User) {
    User.$update();
  }

这是我在后端调用的函数以尝试输入数据,一切似乎都工作正常,除了当我调用 saveUpdates 时,似乎触发了 500 错误。:

function handleError(res, statusCode) {
  statusCode = statusCode || 500;
  return function(err) {
    res.status(statusCode).send(err);
  };
}
// this seems to be the function giving me the problem. 
function saveUpdates(updates) {
  return function(entity) {
    var updated = _.merge(entity, updates);
    return updated.saveAsync()
      .spread(updated => {
        return updated;
      });
  };
}

function respondWithResult(res, statusCode) {
  statusCode = statusCode || 200;
  return function(entity) {
    if (entity) {
      res.status(statusCode).json(entity);
    }
  };
}

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

export function updateUser(req, res) {
  if (req.body._id) {
    delete req.body._id;
  }
User.findByIdAndUpdate(req.params.id)
    .then(handleEntityNotFound(res))
    .then(saveUpdates(req.body))
    .then(respondWithResult(res))
    .catch(handleError(res));
    }

我已尝试在 this 页面上针对 angular-full 堆栈执行以下建议,例如将 ._merge 更改为 ._extend 无济于事。

【问题讨论】:

    标签: javascript angularjs mongoose angular-fullstack mongoose-schema


    【解决方案1】:

    我回答了我的问题:

    Angular-full stack 使用 lodash,所以如果你想像这样使用 ._merge 发出 put 请求:

    function saveUpdates(updates) {
      return function(entity) {
        var updated = _.merge(entity, updates);
        return updated.saveAsync()
          .spread(updated => {
            return updated;
          });
      };
    }
    

    然后您需要将 lodash 导入到由 angular-fullstack yeoman 生成器设置的 user.contoller 中。

    import _ from 'lodash';
    

    【讨论】:

    • 哦,伙计,我一直在试图弄清楚为什么在合并后出现 500 错误,但我完全忽略了排除 lodash!这是小事......
    猜你喜欢
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2011-10-17
    相关资源
    最近更新 更多