Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 6 - reading from mongodb #36

Open
kabu14 opened this issue Jan 3, 2018 · 3 comments
Open

Chapter 6 - reading from mongodb #36

kabu14 opened this issue Jan 3, 2018 · 3 comments

Comments

@kabu14
Copy link

kabu14 commented Jan 3, 2018

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
Copy link
Author

kabu14 commented Jan 3, 2018

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
Copy link

@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
Copy link

@orangesoncom Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants