풀스택 유튜브 클론코딩 강의 #6.15 ~ 6. (230219)
schema : 형태를 정의
- new Video : video model의 database에 저장할 문서 (js Object), 올바르지 않은 형태의 데이터는 저장하지 않는다. video.save()필요
- Video.create : 실제 data를 DB에 저장
[Video.js]
const videoSchema = new mongoose.Schema({
title: String,
description: String,
createdAt: Date,
hashtags: [{ type: String }],
meta: {
views: Number,
rating: Number,
},
});
[videoController.js]
export const postUpload = async (req, res) => {
const { title, description, hashtags } = req.body;
await Video.create({
title,
description,
createdAt: Date.now(),
hashtags: hashtags.split(",").map((word) => `#${word}`),
meta: {
views: 0,
rating: 0,
},
});
return res.redirect("/");
};