videoRouter.get("/:id([0-9a-f]{24})", watch);
videoRouter.route("/:id([0-9a-f]{24})/edit").get(getEdit).post(postEdit);
videoRouter.route("/upload").get(getUpload).post(postUpload);
export const watch = async (req, res) => {
const { id } = req.params;
const video = await Video.findById(id); << <<
return res.render("watch", { pageTitle: video.title, video }); << <<
};
export const watch = 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("watch", { pageTitle: video.title, video });
};
========
[404.pub]
extends base
>> return을 꼭 넣어줘야함 . 안하면 밑에꺼 도 다 실행해버림
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: `Edit ${video.title}`, video });
};
video.hashtags = hashtags
.split(",")
.map((word) => (word.startsWith("#") ? word : `#${word}`));
video.title = title;
video.description = description;
video.hashtags = hashtags
await Video.findByIdAndUpdate(id, {
title,
description,
hashtags: hashtags
.split(",")
.map((word) => (word.startsWith("#") ? word : `#${word}`)),
});
//const video = await Video.findById(id);
const video = await Video.exists({_id:id});
= hooks >> 이용해서 hashtags 깔끔하게 정리하기
videoSchema.pre("save", async function () {
console.log("Wa are about to save:", this);
});
const Video = mongoose.model("Video", videoSchema);
export default Video;
videoSchema.pre("save", async function () {
this.hashtags = this.hashtags[0]
.split(",")
.map((word) => (word.starstWith("#") ? word : `#${word}`));
});
[videos.js]
export const formatHashtags = (hashtags) => {
hashtags.split(",").map((word) => (word.startsWith("#") ? word : `#${word}`));
};
=============
import Video, { formatHashtags } from "../models/Video";
~~
hashtags: formatHashtags(hashtags),
});~~
videoSchema.static("formatHashtags", function (hashtags) {
return hashtags.split(",").map((word) => (word.startsWith("#") ? word : `#${word}`));
});
==============
hashtags: Video.formatHashtags(hashtags),
>> import, export필요가 없음,,쿨,,
ul
each hashtag in video.hashtags
li=hashtag
ch7. User authentication (0) | 2022.08.23 |
---|---|
ch6.25~ delete, search (0) | 2022.08.22 |
ch6.14~ creating video, exception & validation (0) | 2022.08.18 |
ch6.8~ mongo (0) | 2022.08.17 |
ch6. mongoDB & Mongoose (0) | 2022.08.16 |
댓글 영역