Callback is not a function -mongoDB

Hello,

I am working on the final project " Information Security and Quality Assurance Projects - Anonymous Message Board" and getting this error.

TypeError: callback is not a function

at db.collection.find.project.toArray (/app/controllers/handlers.js:100:9)

    at result (/rbd/pnpm-volume/c576d8e3-af34-4559-bcf6-a493b4c94c6a/node_modules/.registry.npmjs.org/mongodb/3.2.7/node_modules/mongodb/lib/utils.js:410:17)

    at executeCallback (/rbd/pnpm-volume/c576d8e3-af34-4559-bcf6-a493b4c94c6a/node_modules/.registry.npmjs.org/mongodb/3.2.7/node_modules/mongodb/lib/utils.js:402:9)

    at handleCallback (/rbd/pnpm-volume/c576d8e3-af34-4559-bcf6-a493b4c94c6a/node_modules/.registry.npmjs.org/mongodb/3.2.7/node_modules/mongodb/lib/utils.js:128:55)

    at cursor.close (/rbd/pnpm-volume/c576d8e3-af34-4559-bcf6-a493b4c94c6a/node_modules/.registry.npmjs.org/mongodb/3.2.7/node_modules/mongodb/lib/operations/cursor_ops.js:224:62)

    at handleCallback (/rbd/pnpm-volume/c576d8e3-af34-4559-bcf6-a493b4c94c6a/node_modules/.registry.npmjs.org/mongodb/3.2.7/node_modules/mongodb/lib/utils.js:128:55)

    at completeClose (/rbd/pnpm-volume/c576d8e3-af34-4559-bcf6-a493b4c94c6a/node_modules/.registry.npmjs.org/mongodb/3.2.7/node_modules/mongodb/lib/cursor.js:898:14)

    at _endSession (/rbd/pnpm-volume/c576d8e3-af34-4559-bcf6-a493b4c94c6a/node_modules/.registry.npmjs.org/mongodb/3.2.7/node_modules/mongodb/lib/cursor.js:909:37)

    at Cursor._endSession (/rbd/pnpm-volume/c576d8e3-af34-4559-bcf6-a493b4c94c6a/node_modules/.registry.npmjs.org/mongodb-core/3.2.7/node_modules/mongodb-core/lib/cursor.js:195:5)

    at Cursor._endSession (/rbd/pnpm-volume/c576d8e3-af34-4559-bcf6-a493b4c94c6a/node_modules/.registry.npmjs.org/mongodb/3.2.7/node_modules/mongodb/lib/cursor.js:231:59)

It seems like something is wrong in somewhere below code but I can not figure out how to solve it.
Handlaer.js

//get most recent 10 bumped threads
exports.getTenThread = function(req, res, callback){
  var board = req.params.board
  var thread_id = req.body.thread_id
  var threadText = req.body.text
  
  
  MongoClient.connect(CONNECTION_STRING, {useNewUrlParser: true}, function(err, client){
    if(err){console.log("Database error: : " + err)}
    console.log("MongoClient connected successfully")
    var record = []
    var db = client.db("MessageBoard");
    db.collection(board).find().sort({bumped_on: -1}).project( { reported: 0, delete_password: 0} ).toArray((err, docs)=>{
      if (err){
        res.json(err);
        console.log("Database error: : " + err);
      };

      for(var i=0; i<10; i++){
                       record.push(docs[i])
                       } 
      callback( null, record);
    });  
  })  
}

api.jp

  .get(function (req, res){
    ThreadController.getTenThread(req, function(err, res){
      console.log("its working")
      console.log("res",res)
      res.json(res);
      
    })
  })

Can anyone help me with this?

here is my glitch link:

Hey there! I’m not too familiar with glitch or that portion of the curriculum, but the first place I would start is to verify the isArray function takes a callback. See here if this helps. If you’re sure the code you’ve written is syntactically correct, I’d move on to console.logging for the full chain of functions. As in:

exports.getTenThread = function(req, res, callback){
  var board = req.params.board
  var thread_id = req.body.thread_id
  var threadText = req.body.text
  
  
  MongoClient.connect(CONNECTION_STRING, {useNewUrlParser: true}, function(err, client){
    if(err){console.log("Database error: : " + err)}
    console.log("MongoClient connected successfully")
    var record = []
    var db = client.db("MessageBoard");
    //Start error checking here:
    console.log('1', db)
    console.log('2', db.collection(board))
    console.log('3', db.collection(board).find({bumped_on: -1))
    console.log('4', db.collection(board).find({bumped_on: -1).project({reported: 0, delete_password: 0}))
    console.log('5', db.collection(board).find({bumped_on: -1).project({reported: 0, delete_password: 0}).toArray())
    //END
    db.collection(board).find().sort({bumped_on: -1}).project( { reported: 0, delete_password: 0} ).toArray((err, docs)=>{
      if (err){
        res.json(err);
        console.log("Database error: : " + err);
      };

      for(var i=0; i<10; i++){
                       record.push(docs[i])
                       } 
      callback( null, record);
    });  
  })  
}

Perhaps if everything runs fine up to .toArray(), then that function itself is producing a null or undefined or some other type rather than a callback

Thanks @huntinghawk1415!
Apparently, it was because of api.js file.

  .get(function (req, res){
    ThreadController.getTenThread(req, res, function(err, data){  //<---missing "res" , and renamed "res" to "record"
      console.log("its working")
      res.json(data)
    })
  })

Since I returned the record data as “res” it overwrite “res” to the record data. By renaming it as “data”, it won’t overwrite “res” and fix the problem.

Thanks!

1 Like