0

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.

1 Answers1

0

HTML forms don't support the PUT method. As it is explained here, the form will be sent as a GET request. That is a probable reason why your router handler is never executed as you are expecting a PUT method while sending a GET request.

In order to fix this, you can set both the router handler and form method to 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.

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. It will always hit /dashboard/users/edit/:id and not /dashboard/users/edit/4832 (assuming 4832 is a real userId).

Here you need to have the actual id in the form action. For example: <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"