TypeError:fcallback不是函数

时间:2017-11-08 18:01:05

标签: node.js

我有以下node.js文件,名为read.js。它给了我以下error,我不明白为什么?

TypeError:函数回调不是函数错误:

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)

这是db-conn.js文件:

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

0 个答案:

没有答案