When you set a rules on the schema

PussinSocks·2022년 7월 22일
0

There are two parts to make specific requirement for the data to be inserted.

const videoSchema = new mongoose.Schema({
    title: {type: String, required: true, trim: true, maxLength:80 },
    description: {type: String, required: true, trim: true, minLength: 20 },
    createdAt: {type: Date, required: true, default: Date.now },
    hashtags: [{type: String, trim: true}],
    meta: {
        views: {type: Number, default: 0, required: true},
        rating: {type: Number, default: 0, required: true},
    },
});

more requirements make the data more specific.

But there are similar requirements we can make in HTML. like min/max legnth for the text or number. Which should we use?
The Answer is BOTH.

The requirements we put on the HTML is for the users.
And also to protect the database.

  1. An URL is requested from a browser.
    https://localhost:4000/
  2. Server listen to the request and finds the URL through router.
    app.use("/", globalRouter); => globalRouter.get("/", home);
  3. a callback function from controller executed on the URL is set.
export const home = async(req, res) => {
    const videos = await Video.find({});
    console.log(videos);
    return res.render("home", { pageTitle: "Home", videos });
};
  1. Controller send out response
    • finds all db elements
    • and render home.pug into browser
  2. home.pug is extended from base.pug, and includes video mixins.
profile
On a journey to be a front-end developer

0개의 댓글