0

I have the following node.js file which is called read.js. And it gives me the following error, and I have no idea why?

TypeError: function callback is not a function error:

at global.db.collection.find.toArray (/Users/Desktop/MANDATORY2/read.js:16:16)

var connection = require(__dirname + '/db-conn.js')

/**************************************************/

findAllCourses = (fcallback) => {
    global.db.collection('students').find({}, { "courses.courseName": true, _id: false }).toArray((err, result) => {

        if (err) {
            var jError = { "status": "error", "message": "ERROR -> student.js -> 001" }
            console.log(jError)
            return fcallback(jError)
        }
        var jOk = { "status": "ok", "message": "student.js -> all courses found -> 000" }
        console.log(jOk)
        console.log(JSON.stringify(result))
        return fcallback(jOk)
    })
    findCoursesWithLetterD(fcallback)
}
/**************************************************/

findCoursesWithLetterD = (fcallback) => {
    var regexp = new RegExp('D');

    global.db.collection('students').find({ "courses.courseName": { $regex: regexp } }, { _id: 0, "courses.courseName": 1 }).toArray((err, result) => {
        var filtered = result.map(student => {
            student.courses = student.courses.filter(course => regexp.test(course.courseName))
            return student
        })
        //console.log(JSON.stringify(filtered))
        var flat = filtered.reduce((p, c) => p.concat(c.courses), [])
        //console.log(JSON.stringify(flat));
        if (err) {
            var jError = { "status": "error", "message": "ERROR -> student.js -> 001" }
            console.log(jError)
            return fcallback(jError)
        }
        var jOk = { "status": "ok", "message": "student.js -> courses found -> 000" }
        console.log(jOk)
        console.log(JSON.stringify(result))
        return fcallback(jOk)
    })
}

connection.doConnection(findAllCourses)

This the db-conn.js file :

var connection = {}

/**************************************************/

connection.doConnection = (fcallback) => {
    mongo = require('mongodb').MongoClient
    global.db = null
    sDatabasePath = 'mongodb://localhost:27017/kea'
    global.mongoId = require('mongodb').ObjectID

    /**************************************************/

    mongo.connect(sDatabasePath, (err, db) => {
        if (err) {
            console.log('ERROR 003 -> Cannot connect to the database')
            return fcallback(err, false);
        }
        global.db = db
        console.log('OK 002 -> Connected to the database')
        return fcallback(false, db);
    })
}

/**************************************************/

module.exports = connection
codeDragon
  • 555
  • 1
  • 8
  • 28
  • I think we'd need to see the contents of the `doConnection` function. – Kirk Larkin Nov 08 '17 at 18:12
  • sure. just edited @Kirk – codeDragon Nov 08 '17 at 18:15
  • `fcallback` in `db-conn.js` is a reference to `findAllCourses`, which is then called with either `err, false` or `false, db`. When the `fcallback(...)` lines in `read.js` run, `fcallback` is either `err` or `false`, which is not a function - hence the error. – Kirk Larkin Nov 08 '17 at 18:20
  • so what could the solution be then? – codeDragon Nov 08 '17 at 18:26
  • There are too many problems to easily answer this on Stack Overflow. I suggest you start with learning a bit more about Node.js style callbacks and go from there. Here's an answer to get you started: https://stackoverflow.com/a/19739852/2630078. Good luck. – Kirk Larkin Nov 08 '17 at 18:33

0 Answers0