-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
insertMany middleware #11531
Comments
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String
});
testSchema.pre('insertMany', (docs, next) => {
console.log('==========================PRE===========================')
console.log('first', docs);
console.log('second', next);
if(Array.isArray(docs)) {
next();
} else {
docs();
}
});
testSchema.post('insertMany', (docs, next) => {
console.log('==========================POST==========================');
console.log('first', docs);
console.log('second', next);
if(Array.isArray(docs)) {
next();
} else {
docs();
}
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.insertMany([{name: 'Test'}, {name: 'Testerson'}]);
}
run(); |
Something to consider changing for Mongoose 7. I agree this is very confusing. |
This is especially confusing in TypeScript code. For example, I have the following code:
Because I assume correcting the order might be a breaking change, but I agree with @vkarpov15 that it should be considered for version 7. |
Dear Mongoose Team,
The post and pre
insertMany
middleware have the arguments order reversed.Pre takes in (next, docs),
Post takes in (docs, next)
Please follow standardized API design or it gets confusing.
The text was updated successfully, but these errors were encountered: