These are chat archives for Automattic/mongoose

7th
Jun 2018
Chris Rutherford
@cjrutherford
Jun 07 2018 16:21

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

Ben Chiciudean
@benydc
Jun 07 2018 16:29
I’m trying to remove an ObjectId from array but it’s not working:
User.findByIdAndUpdate(ctx.session.user._id, { $pull: { events: objectId } })
this is the user document:
Screen Shot 2018-06-07 at 19.30.00.png
objectid is done with:
const objectId = Types.ObjectId(ctx.query.eventId)
Kev
@lineus
Jun 07 2018 16:43
@benydc your syntax looks correct tested here you might need to provide more details, like the values of 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.
Ben Chiciudean
@benydc
Jun 07 2018 16:45
ctx.session.user._id it’s the User Object Id and objectId I create it with the Types.ObjectId(stringID)
maybe Types.ObjectId is not generate the correct ObjectId
Screen Shot 2018-06-07 at 19.47.37.png
I enabled mongoose debug
Ben Chiciudean
@benydc
Jun 07 2018 16:50
I don’t think my update is even called
Kev
@lineus
Jun 07 2018 16:50
is the _id path in your user session schema a string or the default ObjectId?
Ben Chiciudean
@benydc
Jun 07 2018 16:51
it’s the default ObjectId
I used it elsewere and it works fine
Kev
@lineus
Jun 07 2018 16:51
in queries that work, do the objectId's look like the ones in this failing query?
Ben Chiciudean
@benydc
Jun 07 2018 16:52
ok, somehow is working now…..

I used

await User.findByIdAndUpdate(userId, { $pull: { events: objectId } })

userId I pulled from the session

Kev
@lineus
Jun 07 2018 16:56

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
@cjrutherford the only way I could duplicate the error is by removing the module.exports line in the exported model. It should work :confused:
Chris Rutherford
@cjrutherford
Jun 07 2018 17:01
it's okay @lineus , I was missing an 'S' module.export versus module.exports
Kev
@lineus
Jun 07 2018 17:02
cool
Ben Chiciudean
@benydc
Jun 07 2018 17:04
I have a json array and I want to create Schema Objects without using forEach, is that possible?
Kev
@lineus
Jun 07 2018 17:07
@benydc what do you mean by Schema Objects?
Ben Chiciudean
@benydc
Jun 07 2018 17:08
documents
Kev
@lineus
Jun 07 2018 17:09
ah, yeah you can pass an array of objects to Model.create() and it will save them all for you.
Ben Chiciudean
@benydc
Jun 07 2018 17:09
does it return array of documents?
Kev
@lineus
Jun 07 2018 17:09
yup
Nate Mallison
@NJM8
Jun 07 2018 18:23
hey I have a question about errors, I have a route that depends on error messages to communicate issues to the front end, but it isn't working at all in production.
does mongo/mongoose still send error messages when in production?
or are they different in some way?