You are on page 1of 6

Đáp án (SDN301- SU23)

Câu 1:

Sinh viên tạo được schema như dưới đây được 2 điểm :

const mongoose = require("mongoose");


const Schema = mongoose.Schema;

const quizSchema = new mongoose.Schema({


title: String,
description: String,
questions: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Question'
}]
});

const questionSchema = new mongoose.Schema({


text: String,
options: [String],
correctAnswerIndex: Number,
});

Câu 2.

Run chạy route GET/quizzes -> danh sách các news (0.5 điểm)
Run chạy route POST/questions -> tạo 1 document thành công vào News Schema (0.5 điểm)

Run chạy route PUT/news -> thông báo k hỗ trợ và mã lỗi 403 (0.25 điểm)
Run chạy route DELETE/news -> delete 1 document thành công vào News Schema (0.25 điểm)
Câu 3:

quizRouter.route('/:quizId/populate')
.get( function (req, res, next) {
const quizId = req.params.quizId;
Quiz.findOne({ _id: quizId })
.populate({
path: 'questions',
match: { text: { $regex: /capital/i } } // Example
filter condition
})
.exec((err, quiz) => {
if (err) {
console.error("Error finding quiz:", err);
res.status(500).json({ error: 'Internal Server
Error' });
} else if (!quiz) {
res.status(404).json({ error: 'Quiz not found' });
} else {
res.json(quiz);
}
});
});
Câu 4: Thực hiện được xác thực token + 2 điểm

Thêm UserSchema + 1 điểm

var User = new Schema({


username: {
type: String,
default: ''
},
admin: {
type: Boolean,
default: false
}
});

{title: "Science Quiz",


description: "Test your knowledge of scientific concepts!"}

{
text: "What is the capital of France?",
options: ["Paris", "London", "Berlin", "Madrid"],
correctAnswerIndex: 0,
quizId: savedQuiz._id
},
{
text: "Who developed the theory of relativity?",
options: ["Albert Einstein", "Isaac Newton", "Galileo Galilei", "Nikola
Tesla"],
correctAnswerIndex: 0,
quizId: savedQuiz._id
},
{
text: "What is the symbol for iron on the periodic table?",
options: ["Fe", "I", "Au", "Ag"],
correctAnswerIndex: 0,
quizId: savedQuiz._id
}

{
"text": "What is the capital of France?",
"options": ["Paris", "London", "Berlin", "Madrid"],
"correctAnswerIndex": 0

{
"text": "Who developed the theory of relativity?",
"options": ["Albert Einstein", "Isaac Newton", "Galileo Galilei", "Nikola Tesl
a"],
"correctAnswerIndex": 0

You might also like