Skip to content

Commit

Permalink
docs(mongodb): update to mongodb 5
Browse files Browse the repository at this point in the history
As `@fastify/mongodb` updated to mongodb 5, callbacks are not supported anymore and we need to switch to promises
fastify/fastify-mongodb#212
  • Loading branch information
pip77 authored Mar 29, 2023
1 parent f1991cf commit 81223c0
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions docs/Guides/Database.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,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 81223c0

Please sign in to comment.