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

Error handling problem for cursor with noCursorTimeout flag setup #10150

Closed
anotheri opened this issue Apr 16, 2021 · 1 comment
Closed

Error handling problem for cursor with noCursorTimeout flag setup #10150

anotheri opened this issue Apr 16, 2021 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@anotheri
Copy link
Contributor

Do you want to request a feature or report a bug?

I'm reporting a bug

What is the current behavior?

unhandled error thrown from this line: https://github.com/Automattic/mongoose/blob/master/lib/cursor/QueryCursor.js#L60
error stack:

| /opt/app/node_modules/mongoose/lib/cursor/QueryCursor.js:60
|         cursor.close(function() {});
|                ^
|
| TypeError: Cannot read property 'close' of undefined
|     at Array.<anonymous> (/opt/app/node_modules/mongoose/lib/cursor/QueryCursor.js:60:16)
|     at NativeCollection.<computed> [as find] (/opt/app/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:224:30)
|     at /opt/app/node_modules/mongoose/lib/cursor/QueryCursor.js:58:22
|     at /opt/app/node_modules/kareem/index.js:23:7
|     at processTicksAndRejections (internal/process/task_queues.js:76:11)

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

const query = {
  // ↓ this is a `String` typed data field of the Person model
  hireDate: {
    // $lt: '2021-01-01' // correct query, it works well
    
    lt: '2021-01-01' // notice `lt` instead of `$lt` here
    // ↑ this one supposed to throw meaningful error like:
    // > CastError: Cast to string failed for value "{ lte: '2021-04-16' }" at path "hireDate" for model "Person"
  }
};

const cursor = Person
  .find(query)
  .cursor()
  .addCursorFlag('noCursorTimeout', true);
  // w/o `noCursorTimeout ` ↑ flag everything works as expected and meaningful error is shown
  // but if I setup the flag, I'm getting the next error from mongoose (instead of meaningful CastError):
  // > TypeError: Cannot read property 'close' of undefined

await cursor.eachAsync( async ( person ) => {
  // whatever
  return null;
} )

What is the expected behavior?

I expect to get a meaningful error which would point me to the wrong query provided

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

node: 12.14.1
mongo: 4.2
mongoose: ^5.12.4 (latest)

Thanks

@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Apr 19, 2021

const mongoose = require('mongoose');

const personSchema = new mongoose.Schema({
    hireDate: String
});

const Person = mongoose.model('Person', personSchema);


async function test() {
await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  await mongoose.connection.dropDatabase();

  await Person.create({hireDate: '2021-01-00'});
  await Person.create({hireDate: '2021-04-16'});

const query = {
    // ↓ this is a `String` typed data field of the Person model
    hireDate: {
      // $lt: '2021-01-01' // correct query, it works well
      
      lt: '2021-01-01' // notice `lt` instead of `$lt` here
      // ↑ this one supposed to throw meaningful error like:
      // > CastError: Cast to string failed for value "{ lte: '2021-04-16' }" at path "hireDate" for model "Person"
    }
  };
  
  const cursor = Person
    .find(query)
    .cursor()
    .addCursorFlag('noCursorTimeout', true);
    // w/o `noCursorTimeout ` ↑ flag everything works as expected and meaningful error is shown
    // but if I setup the flag, I'm getting the next error from mongoose (instead of meaningful CastError):
    // > TypeError: Cannot read property 'close' of undefined
  
  await cursor.eachAsync( async ( person ) => {
    // whatever
    return null;
  } )
}
test();

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Apr 19, 2021
@vkarpov15 vkarpov15 added this to the 5.12.7 milestone Apr 23, 2021
vkarpov15 added a commit that referenced this issue Apr 28, 2021
titanism added a commit to forwardemail/forwardemail.net that referenced this issue Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

No branches or pull requests

3 participants