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

Transaction's session isn't available in validators #7652

Closed
mateo-gallardo opened this issue Mar 27, 2019 · 3 comments
Closed

Transaction's session isn't available in validators #7652

mateo-gallardo opened this issue Mar 27, 2019 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.

Comments

@mateo-gallardo
Copy link

mateo-gallardo commented Mar 27, 2019

Do you want to request a feature or report a bug?
Bug. May be an enhancement, but a necessary one.

What is the current behavior?
Validator functions don't have access to the session being used in a transaction.

In my case I have a reference validator that checks that the references inserted actually exist in the DB. The problems is that if I insert a document inside a transaction and then use it in a reference field, the validator tries to find it, but fails because it needs to use the session in the query.

Calling this.$session() inside the validator function returns null, so I can't use that.

If the current behavior is a bug, please provide the steps to reproduce.
Standalone script:

const mongoose = require('mongoose');

mongoose.connection.on('error', err => {
  console.log(`MongoDB connection error: ${err}`);
  process.exit(-1);
});

mongoose.connect(
  YOUR_MONGO_URI_HERE,
  {
    keepAlive: 1,
    useNewUrlParser: true,
    useCreateIndex: true
  }
);

(async () => {
  const someSchema = new mongoose.Schema(
    {
      name: String
    },
    { autoCreate: true }
  );
  const SomeModel = mongoose.model('SomeModel', someSchema);

  const someOtherSchema = new mongoose.Schema(
    {
      fieldWithReference: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'SomeModel',
        validate: {
          isAsync: true,
          validator(value, cb) {
            // Need session here
            // this.$session() returns null
            console.log(`this.$session() value: ${this.$session()}`);

            SomeModel.findOne({ _id: value })
              .session(this.$session())
              .exec((err, doc) => {
                if (doc) {
                  console.log('Success! Reference found!');
                } else {
                  console.error('Failed! No reference found!');
                }

                cb(doc);
              });
          }
        }
      }
    },
    { autoCreate: true }
  );
  const SomeOtherModel = mongoose.model('SomeOtherModel', someOtherSchema);
  let session;

  try {
    session = await mongoose.startSession();
    session.startTransaction();
    const [insertedDocument] = await SomeModel.create([{ name: 'Potato' }], { session });
    await SomeOtherModel.create([{ fieldWithReference: insertedDocument }], { session }); // Validator returns false because it can't find insertedDocument
    session.commitTransaction();
  } catch (err) {
    if (session) {
      session.abortTransaction();
    }

    console.error(err);
  }
})();

What is the expected behavior?
Either the validator function should have access to the same session that was passed down to the .create method (probably as a third param), or this.$session() should return that same session.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: 10.15.1
Mongoose: 5.4.9
MongoDB: 4.0.4

I'll get started on a PR this weekend if I can.

Edit: Updated standalone script.

@mateo-gallardo mateo-gallardo changed the title Session isn't available in validators Transaction's session isn't available in validators Mar 27, 2019
@vkarpov15
Copy link
Collaborator

this.$session() should work. If it doesn't, it might just be that it isn't getting set before validators run. Will investigate

@vkarpov15 vkarpov15 added this to the 5.4.24 milestone Mar 29, 2019
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Mar 29, 2019
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Apr 15, 2019
@vkarpov15 vkarpov15 removed this from the 5.5.2 milestone Apr 15, 2019
@yinonby
Copy link

yinonby commented Mar 23, 2021

Can someone please explain how the this.$session() is supposed to work? When I'm trying to access it within the validator, I'm getting my own class member as the reference of "this", and not the schema (which is what I'm guessing the intention is).
I can't find any documentation for it as well.

@yinonby
Copy link

yinonby commented Mar 25, 2021

solved. should not use arrow functions. see https://stackoverflow.com/questions/66768458/how-to-get-mongoose-session-in-mongoose-schema-validator#66798049.

@Automattic Automattic locked and limited conversation to collaborators Mar 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants