2

I cannot seem to connect to my MongoDB. Here is the error that occurs:

RefernceError : Client is not defined . at MongoClient.connect ( C:/user/User/desktop/blog app.js at args.push

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


app.use(bodyParser.urlencoded({extended: true}))


const MongoClient = require('mongodb').MongoClient
var db

MongoClient.connect('mongodb://user:[email protected]:29466/movie-quotes', (err, database) => {
  // ... start the server

   if (err) return console.log(err)
 db = client.db('movie-quotes')
  app.listen(process.env.PORT || 3000, () => {
    console.log('listening on 3000')
  })
})


app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html')
  // Note: __dirname is directory that contains the JavaScript source code. Try logging it and see what you get!
  // Mine was '/Users/zellwk/Projects/demo-repos/crud-express-mongo' for this app.
})


app.post('/quotes', (req, res) => {
  db.collection('quotes').save(req.body, (err, result) => {
    if (err) return console.log(err)

    console.log('saved to database')
    res.redirect('/')
  })

})

2
  • Are you shure you got access to that server? Commented Feb 25, 2018 at 15:29
  • yeah its working now. Just had a quick question as to why this tutorial is not using an mvc pattern ? should i put all the routes in app.js ? or i need to make a controller , model and route folder? Commented Feb 25, 2018 at 15:44

1 Answer 1

1

database is your database client. You should use:

db = database.db('movie-quotes')

instead of:

db = client.db('movie-quotes')
Sign up to request clarification or add additional context in comments.

4 Comments

also can you please quickly explain to me why some tutorials are using routes , controllers models and some arent ? im a bit confused and trying to understand what is the proper way..
The question seems quite wide but I'll try to answer it. Some tutorials use different design patterns (mostly MVC). It is not linked at all with node.js or MongoDB but it is about code architecture. You need to choose the design pattern according to the size and the complexity of your application. Because you said that it is a basic CRUD app, I think an MVC design (routes, controllers, models) seems appropriate and enough for the purpose.
so writing all your code in app.js without a routes and controller folder , is MVC design? also do you have some suggetions on where i can read more about this? thanks
That's quite the opposite of what I have written. MVC Design is about separation. So if you want to implement it, you should not put it all in your app.js but as you said in different files or directories. I can suggest you this repository for some node.js best practices.

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.