Hey all, I'm having an odd issue where I'm getting an error stating that my model isn't a constructor.... did I miss something? I have my Schema Defined, and Model created:
const ModelSchema = new Schema({...});
module.exports = Model = mongoose.model('model', ModelSchmea);
and I'm importing it into my route file and using it like this:
const Model = require('../path/to/model');
router.post('/', (req,res) => {
let newModel = req.body;
new Model(newModel).save().then(...)
});
not sure where the issue is
User.findByIdAndUpdate(ctx.session.user._id, { $pull: { events: objectId } })
const objectId = Types.ObjectId(ctx.query.eventId)
ctx.session.user._id
and objectid
that you are passing in to update and maybe use mongoose.set('debug', true)
to see what mongoose is actually sending in the query.
_id
path in your user session schema a string or the default ObjectId?
I used
await User.findByIdAndUpdate(userId, { $pull: { events: objectId } })
userId I pulled from the session
yeah, that makes sense. ObjectIds have to be 24 hex chars.
node -e "m=require('mongoose');x=m.Types.ObjectId('152838454774-L5EBhCIDshleBtixmyB4epdK6HvYoMRr');console.log(x)"
throws:
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
module.export
versus module.exports
Model.create()
and it will save them all for you.