vasansr / pro-mern-stack

Code Listing for the book Pro MERN Stack
http://www.apress.com/in/book/9781484226520
343 stars 159 forks source link

Chapter 6 - reading from mongodb #36

Open kabu14 opened 6 years ago

kabu14 commented 6 years ago

I installed mongodb via homebrew and I was able to follow the shell examples to work with mongo.

Then in the section 'Reading from MongoDB' I get an error in the server saying: 'TypeError TypeError: db.collection is not a function'. Anyone know how to fix this?

I even tried changing the line to: db.collection('issues').find().toArray() but then it says cannot use find() of undefined.

I checked to see if the database and collection is there and they exist, but just seem to not get the data programmatically.

kabu14 commented 6 years ago

Figured out why.... Apparently if you just run 'npm install mongodb --save' based on what page 101 says you will get the latest version which is 3.0 as of this comment. Version 3 has different ways of getting the database connection. To avoid errors it's best to look at this repository's package.json and use the version that it installs instead of the latest versions.

orangesoncom commented 6 years ago

@wayne-huang14 for mongodb version 3.0 you need some modification like this

MongoClient.connect('mongodb://localhost')
  .then((connection) => {
    db = connection.db('issuetracker');
    app.listen(3000, () => {
      console.log('App start on port 3000');
    });
  })
  .catch((error) => {
    console.log('ERROR', error);
  });

works for me

pythoncreate commented 6 years ago

@orangesoncom Thank you!