课程: Learning Full-Stack JavaScript Development: MongoDB, Node, and React

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Fetching data from MongoDB

Fetching data from MongoDB

- [Narrator] I think it's time for us to start working with MongoDB and get rid of the test data we've been using so far. Delete this file. In API router we don't have test data anymore and we need to get the data from MongoDB in here. So remember the DB file that we created to load the test data? Well, it's also what we need to connect to MongoDB and fetch the data here. We can use the connect client method. So we'll have to import it from this file. Import connect client from ./DB and just like code in the load test data script we need to await on the client. Of course, if the client is already connected we'll get that connected client. Since we're awaiting on something here we'll have to label this function async. And here's the syntax to get data from a connected Mongo client. We do client dot get the collection we're interested in, which is contests. This is actually the only collection we have so far.…

内容