I'm trying to retrieve the _id
of an inserted document in MongoDB
using a callback
function in nodejs
(expressJS
), but I'm encountering this error:
AssignmentDB.save is not a function
Below is my code. Can anyone assist me with retrieving the _id
using a callback function in MongoDB
?
router.route("/assignment/add").post((req, res) => {
let assignmentDb = new AssignmentDB(req.body);
AssignmentDB
.save(assignmentDb, function(err, records){
if(err) throw err;
res.status(200).send(records[0]._id); // this should send the _id of the inserted document
});
});
Here is what my AssignmentDB
model looks like:
const mongoose= require('mongoose');
const Schema= mongoose.Schema;
let AssignmentDB = new Schema({
assignmentName: String,
assignmentDescription: String,
courseName: String,
assignmentDueDate: Date,
isNewAssignment: Boolean
});
module.exports = mongoose.model('AssignmentDB', AssignmentDB, 'AssignmentDB');