I'm having trouble understanding why my mongoose Model.create operation isn't completing successfully.
The same connection is working well with other controller functions.
I am attempting to create a new document, but my code seems to get stuck and I'm unable to identify any errors to troubleshoot the issue.
const createNote = async (req, res) => {
try {
const { user, title, text } = req.body
if (!user || !title || !text) {
return res.status(400).json({ text: 'all fields are required' })
}
console.log('reached this point')
const userId = new mongoose.Types.ObjectId(user)
const newNote = await Note.create({ user: userId, title, text })
console.log("reached this point 1")
if (!newNote) {
return res.status(400).json({ message: 'note not created' })
}
res.status(200).json({ message: `new note created for ${user} ` })
}
catch (e) {
console.error("error handling note creation: ", e);
res.status(500).send()
}
}
const noteSchema = new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User',
},
title: {
type: String,
required: true,
},
text: {
type: String,
default: true,
},
completed: { type: Boolean, default: false },
},
{
timestamps: true,
}
);
noteSchema.plugin(autoIncrement, {
inc_field: 'ticket',
id: 'ticketNums',
start_seq: 500,
});
module.exports = mongoose.model('Note', noteSchema);