简体   繁体   English

更新用户node.js

[英]Update user node.js

I'm trying to allow users to edit their profiles, I have the following currently put this is not working. 我试图允许用户编辑其个人资料,但我目前认为以下内容不起作用。

The form I have like so 我喜欢的形式

<form action="/dashboard/users/edit/:id" method="put">

and I have my route line so 我有路线

// users put
router.put('/dashboard/users/edit/:id', (req, res) => {
  const user = {
    schoolName: req.body.schoolName,
    schoolAddress: req.body.schoolAddress,
    schoolAddress2: req.body.schoolAddress2,
    city: req.body.city,
    zipCode: req.body.zipCode,
    postalAddress: req.body.postalAddress,
    postalCity: req.body.postalCity,
    postalZipCode: req.body.postalZipCode,
    telephone: req.body.telephone,
    email: req.body.email,
    password: req.body.password,
    schoolType: req.body.schoolType,
    schoolDistrict: req.body.schoolDistrict,
    schoolRegion: req.body.schoolRegion,
    curriculum: req.body.curriculum,
    directorName: req.body.directorName,
    directorTelephone: req.body.directorTelephone,
    directorEmail: req.body.directorEmail,
    schoolRepresentativeName: req.body.schoolRepresentativeName,
    schoolRepresentativeTelephone: req.body.schoolRepresentativeTelephone,
    schoolRepresentativeEmail: req.body.schoolRepresentativeEmail,
    schoolRepresentativePosition: req.body.schoolRepresentativePosition,
    schoolRepresentativeTShirt: req.body.schoolRepresentativeTShirt,
    schoolRepresentativeTutorMentor: req.body.schoolRepresentativeTutorMentor
  };

  User.findByIdAndUpdate(req.params.id, user, function(err, raw){
    if(err) {
      res.send(err);
    }
    res.send(raw);
  });
});

I can't see to get the database to update though. 我看不到要更新的数据库。

Any help here is appreciated. 感谢您的帮助。

HTML forms don't support the PUT method. HTML表单不支持PUT方法。 As it is explained here , the form will be sent as a GET request. 由于它是解释在这里 ,表格将被作为一个GET请求。 That is a probable reason why your router handler is never executed as you are expecting a PUT method while sending a GET request. 这就是为什么您的路由器处理程序从不执行的原因,因为您期望发送GET请求时使用PUT方法。

In order to fix this, you can set both the router handler and form method to POST. 为了解决此问题,您可以将路由器处理程序和form方法都设置为POST。

I'm not sure if you have the exact same form code or you put ":id" as part of the action attribute on it for simplicity, but just in case I'll make the following note. 为了简单起见,我不确定您是否具有完全相同的格式代码,或者是否将“:id”作为action属性的一部分放置在该属性上,但是以防万一,我将做以下说明。

A thing to keep in mind is that the ":id" in the form attribute action will not be the actual id of the user you are trying to edit. 要记住的一点是,表单属性操作中的“:id”将不是您要编辑的用户的实际ID。 It will always hit /dashboard/users/edit/:id and not /dashboard/users/edit/4832 (assuming 4832 is a real userId). 它将始终命中/ dashboard / users / edit /:id而不是/ dashboard / users / edit / 4832(假设4832是真实的userId)。

Here you need to have the actual id in the form action. 在这里,您需要在表单操作中具有实际的ID。 For example: <form action="/dashboard/users/edit/4832" method="post"> . 例如: <form action="/dashboard/users/edit/4832" method="post"> This way when Express reads the req.params.id it can retrieve a valid id like 4832 and not ":id" 这样,当Express读取req.params.id时,它可以检索到有效的ID(例如4832),而不是“:id”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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