Home > Software engineering >  Cant access main parents callback function inside mongo query | cb is not a function
Cant access main parents callback function inside mongo query | cb is not a function

Time:12-14

I am working with mongodb in sails js. we are using async.auto from async.js.

I am creating a new mongo connection for few reasons and firing find query to get the data, after retrieving data from db I want to call cb of async.auto to pass the error if any and the result using cb(err, results). but I am getting an error saying cb is not a function.

The Hierarchy is as below:

fun1(cb) -> Mongo.connect -> db.find(results) -> return cb(null, results)

I cant share exact code for some reason but here is and example of my code

Code:

async.auto(
                {
                    fun1: function (cb) {
                           // my logic
                           return cb(null, true);
                    },
                    fun2: [
                        'fun1',
                        function (results, cb) {
                            const querySearch = {
                                id,
                            };

                            MongoClient.connect(url, async function (err, db) {
                                if (err) {
                                    console.error(err);
                                    return cb(err, false);
                                }

                                await db
                                    .collection(colName)
                                    .find(
                                        querySearch,
                                        async function (err, results) {
                                            if (err) {
                                                console.error(err);
                                                return cb(err, false);
                                            }
                                            console.log(results);
                                            return cb(null, results); // Error: cb is not a function
                                        }
                                    );
                                db.close();
                            });
                        },
                    ],
                    fun3: [
                        'fun2',
                        function (results, cb) {
                           return cb(null, true);
                        },
                    ],
                },
                function (err, results) {
                    if(err){
                        console.error(err);
                        res.negotiate('Unexpected Error Occured!')
                    }

                    res.ok('Successfully');
                }
            );

CodePudding user response:

The callback function should be the first parameter of the function.
Also, no need to declare the find callback async:

fun2: [
      'fun1',
      function (cb, results) {
        const querySearch = {
          id,
        };

        MongoClient.connect(url, async function (err, db) {
          if (err) {
            console.error(err);
            return cb(err, false);
          }

          await db
            .collection(colName)
            .find(querySearch, function (err, res) {
              if (err) {
                console.error(err);
                return cb(err, false);
              }
              console.log(res);
              return cb(null, res); 
            });
          db.close();
        });
      },
    ],

CodePudding user response:

It looks like you're running into an issue with variable scope. The cb function that you're trying to call in your code is defined in the outer scope, but you're trying to call it inside the callback to the find function. This means that the cb function is not in scope inside that callback, and you're getting the "cb is not a function" error.

One solution to this problem is to use the bind method to create a new function that has the correct scope. You can do this like this:

await db.collection(colName).find(querySearch, async function(err, results) {
  if (err) {
    console.error(err);
    return cb.bind(this)(err, false); // Bind the correct scope to `cb`
  }

  console.log(results);
  return cb.bind(this)(null, results); // Bind the correct scope to `cb`
});

This will create a new function that has the correct scope for the cb function, and you should be able to call it without any errors.

  • Related