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

Hi, i am unable to use the find documentation. Please help. #10610

Closed
shwe2608 opened this issue Aug 25, 2021 · 11 comments
Closed

Hi, i am unable to use the find documentation. Please help. #10610

shwe2608 opened this issue Aug 25, 2021 · 11 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@shwe2608
Copy link

shwe2608 commented Aug 25, 2021

I tried to create the sample kittens db. I am unable to use the find function as documented.

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/fruitsDB', {useNewUrlParser: true, useUnifiedTopology: true});

const kittySchema = new mongoose.Schema({
  name: String
});

const Kitten = mongoose.model('Kitten', kittySchema);

const silence = new Kitten({ name: 'Silence' });
const fluffy = new Kitten({ name: 'fluffy' });

// silence.save()
// fluffy.save()

Kitten.find(function (err, kittens) {
  if (err) return console.error(err);
  console.log(kittens);
})

Getting the following error message in console :

node kittens.js
TypeError: cursor.toArray is not a function
at model.Query. (/Users/shwetank/Desktop/Web Development/fruitsDBProject/node_modules/mongoose/lib/query.js:2151:19)
at model.Query._wrappedThunk [as _find] (/Users/shwetank/Desktop/Web Development/fruitsDBProject/node_modules/mongoose/lib/helpers/query/wrapThunk.js:27:8)
at /Users/shwetank/Desktop/Web Development/fruitsDBProject/node_modules/kareem/index.js:370:33
at processTicksAndRejections (internal/process/task_queues.js:77:11)
(node:30949) UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments
at Collection.find (/Users/shwetank/Desktop/Web Development/fruitsDBProject/node_modules/mongodb/lib/collection.js:238:19)
at NativeCollection. [as find] (/Users/shwetank/Desktop/Web Development/fruitsDBProject/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:191:33)
at NativeCollection.Collection.doQueue (/Users/shwetank/Desktop/Web Development/fruitsDBProject/node_modules/mongoose/lib/collection.js:135:23)
at /Users/shwetank/Desktop/Web Development/fruitsDBProject/node_modules/mongoose/lib/collection.js:82:24
at processTicksAndRejections (internal/process/task_queues.js:77:11)
(Use node --trace-warnings ... to show where the warning was created)
(node:30949) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:30949) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Please look into it.

@shwe2608 shwe2608 changed the title Hi, i followed the steps on getting started page 2 create a kitten db. Hi, i am unable to use the find documentation. Please help. Aug 25, 2021
@vkarpov15
Copy link
Collaborator

What version of Mongoose are you using? The provided script works as expected in v5.13.7 and v6.0.0, but based on the stack trace you aren't using either of those versions.

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Aug 25, 2021
@VinDotRun
Copy link

VinDotRun commented Aug 26, 2021

I have the same problem. toArray is never used in my code and still get the error TypeError: cursor.toArray is not a function.

I was using Mongoose 5.13.7 with no problems. I upgraded to 6.0.0 and this just happened. Also happens with 6.0.1.

Stack:

[...]\node_modules\mongoose\lib\query.js:2152
    return cursor.toArray(cb);
                  ^

TypeError: cursor.toArray is not a function
    at model.Query.<anonymous> [...]\node_modules\mongoose\lib\query.js:2152:19)
    at model.Query._wrappedThunk [as _find] ( [...]\node_modules\mongoose\lib\helpers\query\wrapThunk.js:27:8)
    at  [...]\node_modules\kareem\index.js:370:33
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

@vkarpov15 vkarpov15 removed the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Aug 26, 2021
@vkarpov15 vkarpov15 added this to the 6.0.2 milestone Aug 26, 2021
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Aug 26, 2021
vkarpov15 added a commit that referenced this issue Aug 26, 2021
@vkarpov15
Copy link
Collaborator

This issue only happens when you call find() before mongoose.connect() succeeds. Fix will be in 6.0.2.

To work around this issue, use await mongoose.connect() to wait for Mongoose to connect before calling find(). Below is the updated find() documentation code that avoids this bug.

const mongoose = require('mongoose');

main().catch(err => console.log(err));

async function main() {
  await mongoose.connect('mongodb://localhost:27017/fruitsDB');

  const kittySchema = new mongoose.Schema({
    name: String
  });

  const Kitten = mongoose.model('Kitten', kittySchema);

  const silence = new Kitten({ name: 'Silence' });
  const fluffy = new Kitten({ name: 'fluffy' });

  const kittens = await Kitten.find();
  console.log(kittens);
}

@lorand-horvath
Copy link
Contributor

lorand-horvath commented Aug 26, 2021

@vkarpov15 So from now on do we always have to await mongoose.connect() before calling find()?
Or is this only a temporary workaround until 6.0.2 is released?

@vkarpov15
Copy link
Collaborator

@lorand-horvath this is only a temporary workaround until we ship 6.0.2. Sorry for the inconvenience!

@lorand-horvath
Copy link
Contributor

lorand-horvath commented Aug 27, 2021

@lorand-horvath this is only a temporary workaround until we ship 6.0.2. Sorry for the inconvenience!

@vkarpov15 I have just tested this and it still errors out even in 6.0.2
I have used this code

mongoose.connect(DB);
(async () => {
    const users = await User.find();
    console.log(users.length);
})().catch(err => console.log(err));

and the error is now different:
(node:5200) UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments ...

If I await mongoose.connect() before User.find(), it works without problems:

(async () => {
    await mongoose.connect(DB);
    const users = await User.find();
    console.log(users.length);
})().catch(err => console.log(err));

Can you please verify? I think something is not working properly in the code which was supposed to fix the issue in 6.0.2: 86f53ad

I've also opened a PR to remove the await before mongoose.connect() in the quickstart #10625 but should be processed only after the actual bug is fixed. Thanks!

@vkarpov15 vkarpov15 reopened this Aug 27, 2021
@vkarpov15 vkarpov15 modified the milestones: 6.0.2, 6.0.3 Aug 27, 2021
@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 confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Aug 27, 2021
@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 Aug 27, 2021
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Aug 27, 2021

All 3 run without issue

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
    name: String
});

const Test = mongoose.model('Test', testSchema);

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

      await Test.create({name: 'Test'});

      await Test.find();
      console.log('Done');
}


test();
const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
    name: String
});

const Test = mongoose.model('Test', testSchema);
mongoose.connect('mongodb://localhost:27017/test', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      });
    

async function test() {
    
      await mongoose.connection.dropDatabase();

      await Test.create({name: 'Test'});

      await Test.find();
      console.log('Done');
}


test();
const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
    name: String
});

const Test = mongoose.model('Test', testSchema);
mongoose.connect('mongodb://localhost:27017/test', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      }).then((res) => console.log('THis statement has executed'));
    

async function test() {
    
      await mongoose.connection.dropDatabase();

      await Test.create({name: 'Test'});

      await Test.find();
      console.log('Done');
}


test();

@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Aug 27, 2021
@vkarpov15
Copy link
Collaborator

Below script repros the issue. We'll work on a fix tomorrow 👍

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
    name: String
});

const Test = mongoose.model('Test', testSchema);
mongoose.connect('mongodb://localhost:27017/test');
    

async function test() {
      await Test.find();
      console.log('Done');
}


test();

@lorand-horvath
Copy link
Contributor

lorand-horvath commented Aug 28, 2021

Below script repros the issue. We'll work on a fix tomorrow +1

@vkarpov15 I already reported it above in #10610 (comment) , including the repro script, see it again below - I guess it wasn't clear enough?
Only difference was that I used an IIFE instead of the named test function as async wrapper. The rest that's not in there is boilerplate anyway, I don't think it adds/changes anything essential :-)

mongoose.connect(DB);
(async () => {
    const users = await User.find();
    console.log(users.length);
})().catch(err => console.log(err));

The point is, this is still a bug and it is reproducible.

@Mgotya
Copy link

Mgotya commented Aug 30, 2021

When this fix will be released? it fixed two day ago but the 6.0.2 release was three days ago so this fix is not a part of 6.0.2

@vkarpov15
Copy link
Collaborator

@Mgotya we will release 6.0.3 with 9de60a7 later today.

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.
Projects
None yet
Development

No branches or pull requests

6 participants