Skip to content
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

Closed
1 task done
marcosmartinez7 opened this issue Mar 17, 2023 · 11 comments
Closed
1 task done

Post save middleware: reading from database the saved file #13186

marcosmartinez7 opened this issue Mar 17, 2023 · 11 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. Stale

Comments

@marcosmartinez7
Copy link

marcosmartinez7 commented Mar 17, 2023

Prerequisites

  • I have written a descriptive issue title

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

FileSchema.post<FileDocument>('save', async function (doc, next) {
    try {
      const fileModel = this.$model('File');

     
      console.log('the id ', doc._id);
      const file = await fileModel.findById(doc._id);
      console.log('The file ', file);

    } catch (err) {
      console.log('ERROR', err);
      next(err);
    }
  },
});


in that hook im just querying the database for the persisted document.

I get null as a result

If 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

@marcosmartinez7 marcosmartinez7 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Mar 17, 2023
@vkarpov15 vkarpov15 added this to the 7.0.3 milestone Mar 17, 2023
@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Mar 17, 2023
@IslandRhythms
Copy link
Collaborator

Please update what your mongoose version is, 9.2.0 is not a thing
I'll be testing on latest

@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Mar 17, 2023
@IslandRhythms
Copy link
Collaborator

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();

@marcosmartinez7
Copy link
Author

marcosmartinez7 commented Mar 17, 2023

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?

@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Mar 21, 2023
@vkarpov15
Copy link
Collaborator

@rahulkumawat30 your comments are off topic. Please open a new issue and follow the issue template.

@vkarpov15
Copy link
Collaborator

@marcosmartinez7 the script runs fine for me against Atlas. Can immediately read the update.

Below is what my connection string looks like:

mongodb+srv://foo:bar@host.XYZ.mongodb.net/db?retryWrites=true&w=majority

Can you please provide what your query string looks like, with credentials and hostname excluded? If you don't have w=majority in the connection string and you set a readPreference, this may explain the issue you're seeing.

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Mar 22, 2023
@vkarpov15 vkarpov15 removed this from the 7.0.3 milestone Mar 22, 2023
@marcosmartinez7
Copy link
Author

marcosmartinez7 commented Mar 23, 2023

Hi @vkarpov15

"mongodb+srv://XXXX:XXXXX@XXXX.mongodb.net/test"

I do not have the w=majority

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

@vkarpov15
Copy link
Collaborator

Are you also setting a read preference anywhere? Can you try and see if you still have this issue with w=majority in your connection string?

@marcosmartinez7
Copy link
Author

Are you also setting a read preference anywhere? Can you try and see if you still have this issue with w=majority in your connection string?

Not really. Just defaults.

I will try to reproduce it with w=majority. Couldn't find any docs that relate that with this issue, what do you have in mind about it?

Regards

@vkarpov15
Copy link
Collaborator

The idea behind w=majority is you're telling MongoDB to not report any write operations (like save()) as complete until the write has replicated to a majority of the replica set. I'm not 100% familiar with how Atlas is configured under the hood, but it is possible that you may need w=majority to avoid this issue. I'd imagine this might be a reason why Atlas always includes w=majority in sample connection strings.

@github-actions
Copy link

github-actions bot commented Apr 8, 2023

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

@github-actions github-actions bot added the Stale label Apr 8, 2023
@github-actions
Copy link

This issue was closed because it has been inactive for 19 days and has been marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. Stale
Projects
None yet
Development

No branches or pull requests

3 participants