-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Post save middleware: reading from database the saved file #13186
Comments
Please update what your mongoose version is, 9.2.0 is not a thing |
No problems for me. See if you can modify this script to demonstrate your issue. const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String
});
testSchema.post('save', async function (doc, next) {
console.log('what is doc', doc);
const model = this.$model('Test');
const res = await model.findById(doc._id);
console.log('what is res', res);
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.create({
name: 'Test Testerson'
});
const doc = await Test.findOne();
doc.name = 'Run Hook';
await doc.save();
console.log('done');
}
run(); |
This works on my local machine but doesn't on MongoAtlas. I forgot to mention that. On mongo atlas it seems that I need to add a timeout to delay the execution of the query and give time to the database to persist the object Does it make sense? |
@rahulkumawat30 your comments are off topic. Please open a new issue and follow the issue template. |
@marcosmartinez7 the script runs fine for me against Atlas. Can immediately read the update. Below is what my connection string looks like:
Can you please provide what your query string looks like, with credentials and hostname excluded? If you don't have |
Hi @vkarpov15 "mongodb+srv://XXXX:XXXXX@XXXX.mongodb.net/test" I do not have the Can you explain me why it is related with the issue? Also my string connection is the "Default" string, so I expected this to work with default configuration. Is not the case? Thanks |
Are you also setting a read preference anywhere? Can you try and see if you still have this issue with |
Not really. Just defaults. I will try to reproduce it with Regards |
The idea behind |
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
This issue was closed because it has been inactive for 19 days and has been marked as stale. |
Prerequisites
Mongoose version
7.0.2
Node.js version
16.0.14
MongoDB version
5.4
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
20.04
Issue
Hello,
Im using
post save
middleware and I dont know which is the expected behavior. Didn't found any official docs about this situation:Basically my FileSchema is implementing a post save
in that hook im just querying the database for the persisted document.
I get
null
as a resultIf I wrap the entire code into a
setTimeout
to give the hook 1 second or so, the file is returned.I was expecting that the post save hook context ensures that the documents are persisted on the database. But it seems that it could happen that there are not present already (the setTimeOut validates that)
Which is the expected behavior?
Regards
The text was updated successfully, but these errors were encountered: