Connect with mongoose using async/await

Nội dung bài viết

Video học lập trình mỗi ngày

Connect with mongoose using async/await

const mongoose = require('mongoose')

mongoose.Promise = Promise

mongoose.connection.on('connected', () => {
  console.log('Connection Established')
})

mongoose.connection.on('reconnected', () => {
  console.log('Connection Reestablished')
})

mongoose.connection.on('disconnected', () => {
  console.log('Connection Disconnected')
})

mongoose.connection.on('close', () => {
  console.log('Connection Closed')
})

mongoose.connection.on('error', (error) => {
  console.log('ERROR: ' + error)
})

const run = async () => {
  await mongoose.connect('mongodb://localhost:27017/test', {
    autoReconnect: true,
    reconnectTries: 1000000,
    reconnectInterval: 3000
  })
}

run().catch(error => console.error(error))

Có thể bạn đã bị missing