[Node JS] #5.3 CRUD(2)
#. Upload
1. Control 404 Error
if (!video) {
return res.render("404", { pageTitle: "Video not found." });
} else {
return res.render("watch", { pageTitle: video.title, video });
}
2. GET
export const getEdit = async (req, res) => {
const { id } = req.params;
const video = await Video.findById(id);
if (!video) {
return res.render("404", { pageTitle: "Video not found." });
}
return res.render("edit", { pageTitle: `Editing ${video.title}`, video });
};
3. POST
export const postEdit = async (req, res) => {
const { id } = req.params;
const { title, description, hastags } = req.body;
const video = await Video.findById(id);
if (!video) {
return res.render("404", { pageTitle: "Video not found." });
}
video.title = title;
video.description = description;
video.hashtags = hashtags
.split(",")
.map((word) => (word.startsWith("#") ? word : `#{word}`));
await video.save();
return res.redirect(`/videos/${id}`);
};
4. findOneAndUpdate()
export const postEdit = async (req, res) => {
const { id } = req.params;
const { title, description, hastags } = req.body;
const video = await Video.findById(id);
if (!video) {
return res.render("404", { pageTitle: "Video not found." });
}
await Video.findByIdAndUpdate(id, {
title,
description,
hastags: hashtags
.split(",")
.map((word) => (word.startsWith("#") ? word : `#{word}`)),
});
return res.redirect(`/videos/${id}`);
};
5. Exists()
const video = await Video.exists({ _id: id });
if (!video) {
return res.render("404", { pageTitle: "Video not found." });
}
6. Check
Reference
この問題について([Node JS] #5.3 CRUD(2)), 我々は、より多くの情報をここで見つけました https://velog.io/@choiish98/Node-JS-5.3-CURD2テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol