pro-mern-stack icon indicating copy to clipboard operation
pro-mern-stack copied to clipboard

Chapter 6 - reading from mongodb

Open kabu14 opened this issue 7 years ago • 3 comments

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 avatar Jan 03 '18 09:01 kabu14

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.

kabu14 avatar Jan 03 '18 19:01 kabu14

@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

orangesoncom avatar Jan 22 '18 06:01 orangesoncom

@orangesoncom Thank you!

pythoncreate avatar Jan 23 '18 03:01 pythoncreate