I have follow the directions in mongoose here
PostSchema.pre('update', function() {
console.log('pre update');
console.log(this);
});
it is not firing this middleware. Am I missing something here?
I have added next so it looks exactly like my pre save, however that still does nothing.
Make sure you don't define this after mongoose.model() has been called. Please also take note that findOneAndUpdate / upserts or updates won't trigger this hook. Another reason why it wouldn't execute is that validation fails. Therefore you would need to setup a pre('validate') hoke
I think you have to add the await keyword before your promise.
Related
I was wondering if I can do something like this in mongoose.
schema.pre('save' || 'update' , function (next) {
//do something
});
Like trigger this middleware either for save or update actions.
Thank you
No, as it will always result in the hook being made as a pre-save hook. Not because of Mongoose, but because of how ECMAScript logical operators work.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR
If you want a pre-save and a pre-validate hook, you would need to create them separately.
There are some fields in my Subdocument schema that I want to validate before saving to the database . However, if they are not valid, instead of failing, I just want to set them to undefined . I Defined the two functions on
ServerSchema.method
But when I try to access them in the hook, I get the error
this.conditionallyRemoveFields Is not a function
However, this only happens when saving the top level document, and is not when saving the server alone . When I save the server a loan, there are no errors . Here is the hook
ServerSchema.pre('save', function(next) {
this.conditionallyRemoveField(this.shouldRemoveField, ["Apple","orange"]);
next();
});
Any idea what might be going on ? Also, is this even the right approach for this problem ? Thanks in advance .
It turns out the solution was to define the methods before defining the parent schema. After doing that, everything worked perfectly.
I need to generate slug every time that post get saved into database. Specifficallly on Post.create and post.save. The single place where I may need this in PostShema.pre('validate') middleware like the following:
PostSchema.pre('validate', function (next) {
this.slug = sluglify(this.title);
return next();
});
All works fine except the fact that it happens in validate middlweare that should only check but not set.
SO my questuin is where should I reside my code for sluglify my title on creating or updating post?
This is not happening in the validation but before the validation. IMHO, it 's like if you are prepearing/cleaning your object before validating it; which is OK.
If you feel more confortable with that you could include it in a pre-save or pre-init instead of the pre-validate
I am sure this is simply a misunderstanding, but I can't figured it out :/
I am trying to update a document in mongoDB, using mongoose on a node server.
My code looks like this:
Message.update(searchQuery, updateQuery, function(err, response)
{
if(err) return handleError(err);
if(response) console.log(util.inspect(response));
});
When I first call this function, the callback is not executed and no changes are applied to the database. Effectively, the update does not happen.
When I call the function a second time, the callback from the first call returns and the changes from the fist update are applied to the DB. The callback for the second call does not return though and no changes for the second call are apllied.
When I call it for the third time, callback 2 returns and changes 2 are applied, but not callback and changes 3. And so on...
I assumed it has something to do with the mongoose function not directly executing when no callback is specified, so I tried adding an empty "options" array:
Message.update(searchQuery, updateQuery, **{}**, function(err, response){...});
or executing the update explicitly:
Message.update(searchQuery, updateQuery).exec( function(err, response){...});
The results were unchanged though.
Missing Mongoose callbacks are typically caused by the update waiting for the connection to be opened, as any calls to update, save, find, etc. will be queued up by Mongoose until the mongoose.connect call has completed.
So ensure your mongoose.connect call is being made before your call to update.
The right way to call update with mongoose is the following:
Message.update(query, update).exec(callback);
Whats exactly in your updateQuery?
I am following almost the exact example for Model.count() from the Mongoose docs:
User.count({ type: 'jungle' }, function (err, count) {
console.log('I do not ever run');
});
This should print 'I do not ever run'. Instead, it returns a Query object - which should not happen, according to the docs, as I am providing a callback. How can I make the callback function run? Is there some circumstances where the callback is not run?
Using mongoose#3.6.17. Thanks!
Make sure you've connected to the database before calling any model functions. Mongoose will just queue up the count query until you connect otherwise.
See this question of the FAQ.