Skip to content

Commit

Permalink
fix(docs): mongodb@5 (#213)
Browse files Browse the repository at this point in the history
* fix(docs): mongodb@5

looking into this merged PR, it seems like you are using mongodb v5.x.x already and callbacks are not working anymore.

* replace reply.send with return

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>

* replace reply.send with return

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>

* rm reply.send

---------

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
  • Loading branch information
pip77 and Eomm committed Mar 29, 2023
1 parent bd1e4c3 commit 7f76135
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Fastify MongoDB connection plugin; with this you can share the same MongoDB conn

Under the hood the official [MongoDB](https://github.com/mongodb/node-mongodb-native) driver is used,
the options that you pass to `register` will be passed to the Mongo client.
The `mongodb` driver is v4.x.x.
The `mongodb` driver is v5.x.x.

If you do not provide the client by yourself (see below), the URL option is *required*.

Expand All @@ -32,19 +32,18 @@ fastify.register(require('@fastify/mongodb'), {
url: 'mongodb://mongo/mydb'
})

fastify.get('/user/:id', function (req, reply) {
fastify.get('/user/:id', async function (req, reply) {
// Or this.mongo.client.db('mydb').collection('users')
const users = this.mongo.db.collection('users')

// if the id is an ObjectId format, you need to create a new ObjectId
const id = this.mongo.ObjectId(req.params.id)
users.findOne({ id }, (err, user) => {
if (err) {
reply.send(err)
return
}
reply.send(user)
})
try {
const user = await users.findOne({ id })
return user
} catch (err) {
return err
}
})

fastify.listen({ port: 3000 }, err => {
Expand Down

0 comments on commit 7f76135

Please sign in to comment.