Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongooseError: Model.create() no longer accepts a callback

M_User
  .create(data, (err) => {
      if (err) res.send({
          kq: 0,
          msg: 'kết nối DB thất bại'
        })
     res.send({
          kq: 1,
          msg: 'Đã thêm thành công'
       })
})   

Error below:

throw new MongooseError('Model.create() no longer accepts a callback');
      ^
MongooseError: Model.create() no longer accepts a callback
at Function.create (C:\Users\ASUS\OneDrive\Máy tính\project-17-12\node_modules\mongoose\lib\model.js:2772:11)
at C:\Users\ASUS\OneDrive\Máy tính\project-17-12\router\R_User.js:101:18
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

This is what used to work for me, using Express.js and Mongoose.

How can I fix it?

like image 880
Thu Hà Nguyễn Avatar asked Sep 11 '26 19:09

Thu Hà Nguyễn


1 Answers

the method in Mongoose no longer accepts a callback as the last argument starting from version 6.0.0. Instead, it returns a promise. You should update your code to use promises to handle the result of the create() method

M_User.create(data)
.then((result) => {
  res.send({ kq: 1, msg: 'Đã thêm thành công' })
})
.catch((err) => {
  res.send({ kq: 0, msg: 'kết nối DB thất bại' })
})
like image 159
Otis Avatar answered Sep 15 '26 18:09

Otis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!