This is my model for Quiz:
import mongoose, { Schema } from "mongoose";
const optionsSchema = new Schema({
option1: String,
option2: String,
});
const quizSchema = new Schema(
{
question: { type: String, required: true },
answer: { type: String, required: true },
options: optionsSchema,
},
{
timestamps: true,
}
);
const Quiz = mongoose.models.Quiz || mongoose.model("Quiz", quizSchema);
export default Quiz;
This is the /api/quiz/route.js
import { NextResponse } from "next/server";
import { connectToDB } from "../../utils/connectDB";
import Quiz from "../../models/quiz";
export async function POST(request) {
const { question, answer, options } = await request.json();
console.log({ question, answer, options });
await connectToDB();
await Quiz.create({ question, answer, options });
return NextResponse.json({ message: "Quiz Created" }, { status: 201 });
}
export async function GET() {
await connectToDB();
const quiz = await Quiz.find();
return NextResponse.json({ quiz });
}
But the inserted document in my collection doesn’t have options field:
What am I doing wrong? I am new to mongodb and mongoose so Please bare with me if its a silly mistake