Node.js Mongoose更新功能不工作

Node.js Mongoose更新功能不工作,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在处理项目中的用户帐户部分。我成功地完成了GET、POST和DELETE方法,现在我错过了PUT 当用户提交更新用户表单时,req.body如下所示 { user: { firstName: 'asas', lastName: 'assa', studentId: '1234', email: 'as@as.as', password: '123' } } 我的模式如下所示: const userSchema = new Schema

我正在处理项目中的用户帐户部分。我成功地完成了GET、POST和DELETE方法,现在我错过了PUT

当用户提交更新用户表单时,
req.body
如下所示

{ user:
   { firstName: 'asas',
     lastName: 'assa',
     studentId: '1234',
     email: 'as@as.as',
     password: '123' 
    }
}
我的模式如下所示:

const userSchema = new Schema({
  cuid: { type: 'String', required: true },
  firstName: { type: 'String', required: true },
  lastName: { type: 'String', required: true },
  studentId: { type: 'Number', required: true },
  password: { type: 'String', required: true },
  email: { type: 'String', required: true },
  dateAdded: { type: 'Date', default: Date.now, required: true },
  lastLogin: { type: 'Date', default: null, required: false },
});
最后,我的更新函数如下所示

export function updateUser(req, res) {

  console.log(req.body)
  firstName = sanitizeHtml(req.body.user.firstName );
  lastName = sanitizeHtml(req.body.user.lastName);
  studentId = sanitizeHtml(req.body.user.studentId);
  email = sanitizeHtml(req.body.user.email);
  password = sha512(req.body.user.password).toString('hex');

  let update = { firstName, lastName, studentId, email, password };

  User.findOneAndUpdate(req.params.cuid,update,function(err,updated){
    if(error){
      return res.status(500).send(err);
    }else{
      return res.json({ user: updated });
    }
  });
}

我不明白为什么我的put方法不起作用,也许第二双眼睛可以看到缺陷。

您使用的findOneAndUpdate结构不正确。首先定义,在此基础上搜索id

export function updateUser(req, res) {

  console.log(req.body)
  firstName = sanitizeHtml(req.body.user.firstName );
  lastName = sanitizeHtml(req.body.user.lastName);
  studentId = sanitizeHtml(req.body.user.studentId);
  email = sanitizeHtml(req.body.user.email);
  password = sha512(req.body.user.password).toString('hex');

  let update = { firstName, lastName, studentId, email, password };

 User.findOneAndUpdate({_id: req.params.cuid}, {$set: updated}, function (err, user) {//correct structure
    if(error){
      return res.status(500).send(err);
    }else{
      return res.json({ user: updated });
    }
  });
}

希望这有帮助。

您使用的findOneAndUpdate结构不正确。首先定义,在此基础上搜索id

export function updateUser(req, res) {

  console.log(req.body)
  firstName = sanitizeHtml(req.body.user.firstName );
  lastName = sanitizeHtml(req.body.user.lastName);
  studentId = sanitizeHtml(req.body.user.studentId);
  email = sanitizeHtml(req.body.user.email);
  password = sha512(req.body.user.password).toString('hex');

  let update = { firstName, lastName, studentId, email, password };

 User.findOneAndUpdate({_id: req.params.cuid}, {$set: updated}, function (err, user) {//correct structure
    if(error){
      return res.status(500).send(err);
    }else{
      return res.json({ user: updated });
    }
  });
}

希望这能有所帮助。

我认为您需要将update参数设置为一个对象

第一个参数是查询对象。例如{firstName:“whatever”}。 第二个参数包含更新

例如,如果您试图更新一个用户的姓氏,并按其名称进行搜索,则应该有如下内容

findOneAndUpdate({firstName:req.body.firstName},{lastName:req.body.lastName},function...})
findOneAndUpdate({cuid:req.body.cuid},{$set:{firstName:req.body.firstName,studentId:req.body.studentId...}),function...})
我相信您正在尝试通过id进行搜索,因此您应该输入以下内容

findOneAndUpdate({firstName:req.body.firstName},{lastName:req.body.lastName},function...})
findOneAndUpdate({cuid:req.body.cuid},{$set:{firstName:req.body.firstName,studentId:req.body.studentId...}),function...})

我希望我的回答对您有所帮助。

我相信您需要将update参数设置为一个对象

第一个参数是查询对象。例如{firstName:“whatever”}。 第二个参数包含更新

例如,如果您试图更新一个用户的姓氏,并按其名称进行搜索,则应该有如下内容

findOneAndUpdate({firstName:req.body.firstName},{lastName:req.body.lastName},function...})
findOneAndUpdate({cuid:req.body.cuid},{$set:{firstName:req.body.firstName,studentId:req.body.studentId...}),function...})
我相信您正在尝试通过id进行搜索,因此您应该输入以下内容

findOneAndUpdate({firstName:req.body.firstName},{lastName:req.body.lastName},function...})
findOneAndUpdate({cuid:req.body.cuid},{$set:{firstName:req.body.firstName,studentId:req.body.studentId...}),function...})

我希望我的回答对你有帮助。

你很接近了。问题是您没有正确地传递id。MongoDB正在寻找对象形式的标识符。你写过:

User.findOneAndUpdate(req.params.cuid,update,function(err,updated){
您需要做的是将参数分离为单独的对象:

User.findOneAndUpdate({ _id: req.params.cuid }, { $set: update }, function(err, updated) {

此外,您需要使用
$set
,否则将覆盖整个对象
$set
告诉Mongo只更新通过
update
对象指定的字段,您在上面几行定义了该对象。

您已经接近了。问题是您没有正确地传递id。MongoDB正在寻找对象形式的标识符。你写过:

User.findOneAndUpdate(req.params.cuid,update,function(err,updated){
您需要做的是将参数分离为单独的对象:

User.findOneAndUpdate({ _id: req.params.cuid }, { $set: update }, function(err, updated) {
此外,您需要使用
$set
,否则将覆盖整个对象
$set
告诉Mongo只更新通过
update
对象指定的字段,您在上面定义了几行