0

I am trying to connect mongodb to my express nodejs web application. I am fresh new to nodejs. I am following this tutorial video https://www.youtube.com/watch?v=eB9Fq9I5ocs but I couldn't complete it due to the connection of mongodb.

the app.js code I have:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');



Genre = require('./models/genre');

mongoose.connect('mongodb://localhost/bookstore');


mongoose.connection.on('error', (err) => {
console.error('mongodb connection error', err);
});

mongoose.connection.on('connected', () => {
console.info(`Connected to mongodb`);
});

mongoose.connection.on('disconnected', () => {
console.info('Disconnected from mongodb');
});
// var mongoose = connect('mongodb://localhost/bookstore');
// var db = mongoose.connection;

app.get('/', function(req, res){
res.send('Hello World');
});

app.get('api/genres', function(req , res){
Genre.getGenres(function(err, genres){
    if(err){
        throw err;
    }
    res.json(genres);
})
});

app.listen(3666);
console.log('Server Running On http://localhost:3666');

and this is the genre.js

var mongoose = require('mongoose');


var genreSchema = mongoose.Schema({
name:{
    type: String,
    requires: true
},
create_date:{
    type: Date,
    default: Date.now
 }
});

var Genre = module.exports = mongoose.model('Genre', genreSchema);

module.exports.getGenres = function(callback, limit){
Genre.find(callback).limit(limit);

}

and this is a picture of the database in the terminal

Monogdb shall

The Error that I got: enter image description here

I know this is a basic question but I couldnt figured out I check on google there are others way to connect to the database but I need to know why this particular way which I just followed from the tutorial video havent worked. As you noticed I am a new to nodejs web development so if you could suggest websites or youtube channels to get me start it I would appreciate it.

1
  • just use conn('mongodb://localhost/bookstore'); Commented Aug 12, 2017 at 18:49

1 Answer 1

1

Instead of undefined connect function you should use mongoose.connect(...):

mongoose.connect('mongodb://localhost/bookstore');

But this function is deprecated, you could silence the warning, setting { useMongoClient: true } in options, but it's not recommended.

The best way is to use mongoose.connection object and its openUri method:

let conn = mongoose.connection;
conn.openUri('mongodb://localhost/bookstore');

conn.on('error', err => console.error('mongodb connection error', err));

conn.on('connected', () => console.info(`Connected to mongodb`));

conn.on('disconnected', () => console.info('Disconnected from mongodb'));
Sign up to request clarification or add additional context in comments.

8 Comments

I couldn't write the error here I'll edit it in the question
Updated the answer.
what exactly works with no errors: mongoose.connect('mongodb://localhost/bookstore', { useMongoClient: true, }); if you would to change your answer I would appreciate it to be reference for others. thanks
updated the answer again, using mongoose.connection is deprecated, you should use mongoose.connection.openUri instead.
I got this error ReferenceError: logger is not defined
|

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.