I’m creating a simple MERN stack project – the user makes notes with a note model that includes an ObjectId user ref while testing an endpoint it gives me a request sending with no response I need to know why.
here is my code below.
//Note model
const mongoose = require('mongoose');
const AutoIncrement = require('mongoose-sequence')(mongoose);
//create employee schema
const noteSchema = new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User',
},
title: {
type: String,
required: [true, 'title note is required!'],
},
text: {
type: String,
required: [true, 'note description is required!'],
},
completed: {
type: Boolean,
default: false,
},
},
{
timestamps: true,
}
);
noteSchema.plugin(AutoIncrement, {
inc_field: 'ticket',
id: 'ticketNums',
start_seq: 500,
});
module.exports = mongoose.model('Note', noteSchema);
createNote controller code:
please note that req.id was taken from jwt access token.
const createNote = asyncHandler(async (req, res) => {
const { title, text } = req.body;
if (!title || !text)
return res.status(400).json({
message: 'title and text are required for creating a new note.!',
});
const note = await Note.create({
user: req.id,
title,
text,
});
return res.status(201);
});