1

I am running this code:

const mongoose = require('mongoose');
const DB_NAME = 'eventdb';
const HOST = '127.0.0.1:27017';
mongoose.createConnection(`mongodb://${HOST}/${DB_NAME}`,{useNewUrlParser:true});
const db = mongoose.connection;
db.on('error', console.log('Didn\'t connect properly!'));

And I am getting this output:

Didn't connect properly! events.js:111 throw new ERR_INVALID_ARG_TYPE('listener', 'Function', listener); ^

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received >undefined at checkListener (events.js:111:11) at _addListener (events.js:348:3) at NativeConnection.addListener (events.js:406:10) at Object. (/Users/sebastianc/Desktop/codeworks/exercises/assessments/weekly->assessment-6/server/Models/event-model.js:11:4)

can anyone shed some light on what is going on?

2 Answers 2

2

You have to pass a callback function:

db.on('error', () => console.log('Didn\'t connect properly!'));

See related post on this: TypeError [ERR_INVALID_ARG_TYPE]

Sign up to request clarification or add additional context in comments.

2 Comments

How did you debug that? was it experience? because the stack trace does not refer to that line of code at all
Also stumbled over this myself sometimes in the past. There were also not many options on your code to apply the error message accordingly
0

I would suggest go with connecting with mongoose like this.

mongoose.connect(`mongodb://${HOST}/${DB_NAME}`,{useNewUrlParser:true}, function (err) {
    if (err) {
        console.log('Mongo Error ' + err);
    } else {
        console.log('MongoDB Connection Established');
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.