0

I am getting this error in my code:

/home/ubuntu/workspace/server.js:43
db.collection('quotes').find().toArray(function(err, results) {
  ^
TypeError: Cannot read property 'collection' of undefined
    at Object.<anonymous> (/home/ubuntu/workspace/server.js:43:3)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

My code

const express = require('express');
const bodyParser= require('body-parser')
var MongoClient = require('mongodb').MongoClient
const app = express();
 app.use(bodyParser.urlencoded({extended: true}))

app.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function() {
  console.log("Nodejs Dev Server is Up.");
});

app.get('/',function(req,res){
 res.sendfile(__dirname + '/index.html')
})
var db
MongoClient.connect('mongodb://admin:admin@ds019856.mlab.com:19856/star-wars-quote', (err, database) => {
  if (err) return console.log(err)
  db = database
  app.listen(3000, () => {
    console.log('listening on 3000')
  })
})

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('/')
  })
})

app.get('/', (req, res) => {
  var cursor = db.collection('quotes').find()
})

db.collection('quotes').find().toArray(function(err, results) {
     if (err) return console.log(err)
  console.log(results)
  // send HTML file populated with quotes here
})
Daniel
  • 10,641
  • 12
  • 47
  • 85
  • 1
    You are calling the `db.collection('quotes').find().toArray` before it has established a connection to the database. – cbass Sep 02 '16 at 11:24

1 Answers1

0

To I see two problems in your code here: 1) you are trying to use db.collection before the db variable is assigned, and 2) you want to send HTML without listening for a request.

Problem #1

To the problem here is that you are trying to call db.collection before the database connection is established, thus the db variable has not yet been assigned.

So if the goal is to fetch your quotes upon initialisation of the server you can solve the problem be moving the code into your MongoClient.connect function callback like so:

MongoClient.connect('mongodb://admin:admin@ds019856.mlab.com:19856/star-wars-quote', (err, database) => {
   if (err) return console.log(err)
   db = database

   db.collection('quotes').find().toArray(function(err, results) {
      if (err) return console.log(err)
      console.log(results)
   })
})

Problem #2

In the end of your code example you've got the following lines stating: send HTML file populated with quotes here. But it is not wrapped in a request callback, which will not work, as it needs a request to respond with HTML content (as you have done above).

db.collection('quotes').find().toArray(function(err, results) {
    if (err) return console.log(err)
    console.log(results)
    // send HTML file populated with quotes here
})

Suggestion

app.get('/your-quotes-endpoint', (req, res) => {
    db.collection('quotes').find().toArray(function(err, results) {

       //if you get an error here you should
       //probably send an error response.
       if (err) return console.log(err)
       console.log(results)

       //Generate html
       res.send(/* pass in html string*/)
    })
})

It's also worth mentioning that you define the root endpoint app.get('/') twice, and you call app.listen twice.

cbass
  • 2,548
  • 2
  • 27
  • 39