Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
How would I know if "err" argument is needed in the callback?

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

})

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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); });
})
about 4 years ago · Juan Pablo Isaza Denunciar

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
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda