mongoose代码在特定区域不起作用。

huangapple go评论96阅读模式
英文:

mongoose code not working i specific area

问题

I'm writing a mongoose code, but I get an error when I am using the find() method. The error says the function no longer accepts a callback.

I tried to solve it with other functions, but nothing worked.

英文:

am writing a mongoose code, but i get an error when am using find() method, the error says the functions is no longer accepting a callback.

tried to solve it with other functions but nothing worked

code image here

thats the error type :

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

答案1

得分: 1

在mongoose 7.x中,不再支持回调方法。您可以在这里找到从6.x迁移到7.x的迁移说明:https://mongoosejs.com/docs/migrating_to_7.html#dropped-callback-support

这对于您当前的方法意味着什么?并不太复杂。您只需要使用async/await

app.get('/', async (req, res) => {
  try {
    const myTask = await task.find({});
    // ...您的其余代码
  } catch (error) {
    // ...在此处处理错误
  }
});
英文:

In mongoose 7.x, the support for callback methods was dropped. You can find the migration instructions from 6.x to 7.x here: https://mongoosejs.com/docs/migrating_to_7.html#dropped-callback-support

What does that mean for your current method? Nothing too complicated. You just need to use async/await

app.get('/', async (req, res) => {
  try {
    const myTask = await task.find({});
    // ...rest of your code
  } catch (error) {
    // ...handle error here
  }
});

huangapple
  • 本文由 发表于 2023年6月12日 16:16:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定