英文:
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
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
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论