0

I am a beginner in mongodb and javascript.

I have here a code for simple find method in Mongodb Nodejs. This code does not work.

The solution is to change the last line to this: cursor.limit(10).toArray((err, docs) => console.log(docs));.

What I do not understand is why do I need to include the err argument here? Here is the documentation for toArray method. It does not say here that err parameter is needed. How would I know that err argument is needed here or in other methods?

Thank you very much! I think this is a very basic concept and I will appreciate any input.

const client = new MongoClient(uri);
client.connect()
.then(() => {
  const db = client.db('sample_mflix');
  const coll = db.collection('movies');

  const cursor = coll.find();
  cursor.limit(10).toArray((docs) => console.log(docs));

})
user587266
  • 31
  • 4
  • "*It does not say here that err parameter is needed.*" the documentation states that you have to pass [`Callback`](https://mongodb.github.io/node-mongodb-native/4.1/modules.html#Callback). – VLAZ Sep 29 '21 at 15:09
  • you should always have the error argument, not only when there are errors. – Kevin B Sep 29 '21 at 15:28
  • You have to check either the docs or the source to know what arguments will be passed to the callback. Declaring the wrong arguments will still run in plain Javascript (not in TypeScript), but you will either be missing arguments to the callback or things will be passed in the wrong argument. – jfriend00 Sep 29 '21 at 15:48

2 Answers2

1

In the documentation you linked, we can see that FindCursor.toArray has two valid syntax permutations:

  1. toArray(): Promise<TSchema[]>
  2. toArray(callback: Callback<TSchema[]>): void

Since you are using the second, we need to look at the documentation for Callback. It says there is only one valid syntax:

  1. Callback<T>: (error?: AnyError, result?: T) => void

You must include the error parameter to have a valid Callback, then, and you must have a valid Callback to use the toArray(:Callback) permutation.


The typical way to convey that you don't care about the value of a parameter is to use an underscore (_) as the parameter name.

const client = new MongoClient(uri);
client.connect()
    .then(() => {
        const db = client.db('sample_mflix');
        const coll = db.collection('movies');

        const cursor = coll.find();
        cursor.limit(10).toArray((_, docs) => { console.log(docs); });
})

If you wanted to use the first permutation (which returns a Promise), it would look like the following. This is how you're already handling MongoClient.connect.

const client = new MongoClient(uri);
client.connect()
    .then(() => {
        const db = client.db('sample_mflix');
        const coll = db.collection('movies');

        const cursor = coll.find();
        cursor.limit(10).toArray()
            .then(docs => { console.log(docs); });
})
D M
  • 5,769
  • 4
  • 12
  • 27
0

actually the docs say's to pass a callback.have you checked what arrguments that callback take?? yes , it's error and result.

toArray(callback: Callback<TSchema[]>): void
Callback<T>: (error?: AnyError, result?: T) => void
Ganesh Mohanty
  • 279
  • 1
  • 9