此代码不起作用,它给出错误“模型。findOne()不再接受回调”。修复错误而不降级我的 Mongoose 版本。
`router.post('/ login',async(req,res)=〉{
const email = req.body.email;
const password = req.body.password;
const userType = req.body.userType;
let userSchema;
let redirectUrl;
// Set the user schema and redirect URL based on the user type
if (userType === 'customer') {
userSchema = Customers;
redirectUrl = '/customer/home';
} else if (userType === 'hotel_owner') {
userSchema = HotelOwners;
redirectUrl = '/hotel_owner/home';
} else if (userType === 'advertiser') {
userSchema = Advertisers;
redirectUrl = '/advertiser/home';
} else if (userType === 'destination_manager') {
userSchema = DestinationManagers;
redirectUrl = '/destination_manager/home';
} else if (userType === 'admin') {
userSchema = Admin;
redirectUrl = '/admin/home';
}
// Find the user in the corresponding schema
await userSchema.find({ email: email, password: password })
.then(user => {
if (!user) {
// If user not found, return error message
res.status(401).send({
message: "Invalid email or password"
});
} else {
// Redirect user to their respective home page
res.redirect(redirectUrl);
}
})
.catch(err => {
// Handle errors
console.error(err);
res.status(500).send({
message: "An error occurred"
});
});
});`
如何修复错误并运行此代码
1条答案
按热度按时间tquggr8v1#
既然你已经在使用
await
了,就像这样做吧:另外,我相信您必须使用模型而不是模式来调用像
find()
这样的函数