UPDATE ROUTE

EDIT.EJS

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Edit chat</title>
  </head>
  <body>
    <form method="post" action="/chats/<%=chat._id%>?_method=put">
    <h3>Edit Chat: <%= chat._id %></h3>
    <p>Chat sent from:<b><%= chat.from %></b> to <b><%= chat.to %></b></p>
    <textarea name="msg" rows="7" cols="25"> <%= chat.msg %></textarea>
    <button>Save</button>
</form>
  </body>
</html>

INDEX.JS

const methodOverride = require("method-override");
app.use(methodOverride("_method"));


//UPDATE ROUTE
app.put("/chats/:id",async (req, res) => {
  let { id } = req.params;
  let { msg:newMsg } = req.body;
  let updatedmsg = await Chat.findByIdAndUpdate(
    id,
    { msg:newMsg },
    { new: true, runValidators: true }
  );
  console.log(updatedmsg);
  res.redirect("/chats");
});


Comments

Popular posts from this blog

DELETE ROUTE

CREATE ROUTE

Schema type options