Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssertionError [ERR_ASSERTION]: handler (func) is required in mongodb

I am using mongooose to connect mongodb but i am getting following error

/Users/uchitkumar/api/node_modules/mongodb/lib/mongo_client.js:804
          throw err;
          ^

AssertionError [ERR_ASSERTION]: handler (func) is required
    at new AssertionError (internal/errors.js:315:11)
    at _toss (/Users/uchitkumar/api/node_modules/assert-plus/assert.js:22:11)
    at Function.out.(anonymous function) [as func] (/Users/uchitkumar/api/node_modules/assert-plus/assert.js:122:17)
    at process (/Users/uchitkumar/api/node_modules/restify/lib/server.js:1352:20)
    at argumentsToChain (/Users/uchitkumar/api/node_modules/restify/lib/server.js:1361:12)
    at Server.serverMethod [as put] (/Users/uchitkumar/api/node_modules/restify/lib/server.js:1475:21)

my code for connection is as follow

server.listen(config.port, function() {

    mongoose.connection.on('error', function(err) {
        console.log('Mongoose default connection error: ' + err)
        process.exit(1)
    })

    mongoose.connection.on('open', function(err) {

        if (err) {
            console.log('Mongoose default connection error: ' + err)
            process.exit(1)
        }

        console.log(
            '%s v%s ready to accept connections on port %s in %s environment.',
            server.name,
            config.version,
            config.port,
            config.env
        )

        require('./routes')

    })

    global.db = mongoose.connect(config.db.uri)

})

routes code

server.get('/', function indexHTML(req, res, next) {
    fs.readFile(__dirname + '/../index.html', function (err, data) {
        if (err) {
            next(err);
            return;
        }

        res.setHeader('Content-Type', 'text/html');
        res.writeHead(200);
        res.end(data);
        next();
    });
});

This was fine ... I changed something and now it stopped working with this error. The error is that it is not able to assert some function... in mongodb client. it needed a function. Is it asking to add some handler function? where to add that Thank in advance

like image 609
Uchit Kumar Avatar asked Oct 17 '22 18:10

Uchit Kumar


1 Answers

handler (func) is required is an error that is thrown by restify if one of your routes or middlewares is undefined.

For example:

server.put('/foo/');

This would also trigger it:

var myMidelware = undefined; // todo: define this

app.put('/route', myMiddleware, (req, res) => { /* todo: handle req */ })

That will throw the error handler (func) is required when it tries to validate that myMidelware is a function.

I don't see that in your posted routes code, but I think it's happening somehow. Do you have a PUT method defined somewhere?

(The same error would also happen with server.get(), server.post(), etc, but the [as put] in the stack trace indicates that it's choking on a server.put() call.)

See https://github.com/restify/node-restify/blob/v7.2.1/lib/server.js#L1386

Also, I don't believe the error has anything to do with mongodb; mongo is just in the stack because you run require('./routes') in the mongo connection open handler. The error is coming from your routes file. Annoyingly, mongo's error handling is loosing part of the stack trace. If you moved require('./routes') to outside of the mongo stuff, it would give you the proper stack trace.

like image 54
Nathan Friedly Avatar answered Oct 21 '22 01:10

Nathan Friedly