New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mongoose updateOne add pre then no return #12836
Comments
I guess, As you are using await and async and javascript is a Single-Threaded & Synchronous Language it just execute the code line by line very fast. As you are using async and await which internally deals with Event loop/ TaskQueue/MicroTask. Just waiting for call stack to empty. Why don't you make a the console.log inside a settimeout function and see it, I feel it works when you use a |
The following prints 'use strict';
const mongoose = require('mongoose');
mongoose.set('debug', true);
const schema = mongoose.Schema({ name: String });
schema.pre('updateOne', async function(doc, next) {
this._update = this._update || {};
this._update.$set = { name: 'foo' };
//return next();
next();
});
const Test = mongoose.model('Test', schema);
run().catch(err => console.log(err));
async function run() {
await mongoose.connect('mongodb://localhost:27017/mongoose_test');
const res = await Test.updateOne();
console.log(res);
process.exit(0);
} |
…k can mess up hook execution Fix Automattic/mongoose#12836
The issue is that schema.pre('updateOne',async function (next) { // <-- remove `doc` as first parameter here
const updateInfo = {}
// do stuff...
this._update.$set = { ...updateInfo }
return next()
}) The issue is that Mongoose calls the updateOne hook with |
fix: upgrade kareem -> 2.5.1, Fix #12836
Prerequisites
Mongoose version
6.5.1
Node.js version
v14.17.3
MongoDB version
4.2
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
12.4
Issue
why the result is undefined
The text was updated successfully, but these errors were encountered: