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
"insertOne" callback gets called twice on error. #10956
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
|
IslandRhythms
added
the
needs clarification
This issue doesn't have enough information to be actionable. Close after 14 days of inactivity
label
Nov 8, 2021
This is not a complete code sample - it's a fragment. I'm just showing that on-error, the callback from "insertOne" is called twice. I can simplify it to:
Does that help with clarifying the problem? |
IslandRhythms
added
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
and removed
needs clarification
This issue doesn't have enough information to be actionable. Close after 14 days of inactivity
labels
Nov 8, 2021
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String
});
const Test = mongoose.model('Test', testSchema);
const OtherModel = mongoose.model('OtherModel', testSchema);
async function test() {
await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
useUnifiedTopology: true,});
await mongoose.connection.dropDatabase();
await Test.create({ name: 'Daniel '});
mongoose.connection.close();
mongoose.connection.on('disconnected', () => {
Test.collection.insertOne(OtherModel, {}, (err, result) => {
if(err) return console.log('should show twice');
});
});
}
mongoose.connection.on('open', () => {
console.log('open fire');
});
mongoose.connection.on('connected', () => {
console.log('connected successfully');
});
test(); |
Thank you for your code sample - as you can see, it behaves as I described:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
"insertOne" callback gets called twice on error. Although I suspect the bug affects all collection APIs.
If the current behavior is a bug, please provide the steps to reproduce.
Call "insertOne" after disconnecting from the database to force an error. Notice that the callback gets called twice.
I have isolated the issue to here:
drivers\node-mongodb-native\collection.js
Line 214:
Higher up the file (line 162) you can see that the "callback" already calls "lastArg":
What is the expected behavior?
Callback only gets called once.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose 6.0.12
Node 14.17.0
The text was updated successfully, but these errors were encountered: