我有一个 Express.js 路由,可以通过用户名和密码从我的 MongoDB 数据库中检索用户。我的目标是从请求正文中获取用户名和密码,通过用户名找到用户,然后验证密码是否匹配。如果密码匹配,我想返回用户对象和 200 状态码,如果密码不匹配,我想返回 404 和错误消息。
目前我的路线正在运行,但即使密码不匹配,用户也会始终返回。我正在检查它们是否匹配并抛出错误,但它似乎没有像我预期的那样抛出。理想情况下,如果密码在我的服务调用中不匹配,我想抛出一个错误,并在我的路由中捕获它以返回正确的状态代码。
我试图在我的路线中捕获错误并从服务返回被拒绝的承诺,但仍然返回用户。我的预感是在findOne返回记录后运行接受的回调?
我在控制台中收到 UnhandledPromiseRejectionWarning 对应的行,如果密码不匹配,我将抛出错误:
app_1 | GET /user 200 221 - 4.297 ms
app_1 | (node:257) UnhandledPromiseRejectionWarning: Error: Password does not match
app_1 | at /usr/src/app/services/userService.ts:17:37
app_1 | at Generator.next (<anonymous>)
app_1 | at fulfilled (/usr/src/app/services/userService.ts:5:58)
服务器.ts
注意
我find在导入时重命名为findUservia
find as findUser
app.get('/user', authenticateToken, async (req: Request, res: Response): Promise<void> => {
const user = await findUser(req.body.username, req.body.password);
if (!user) {
res.status(404).send();
return;
}
res.status(200).send({ user });
});
用户服务.ts
const find = (username: string, password: string) => {
return User.findOne({ username }, async (err: Error, user: any) => {
if (err) throw err;
const passwordMatches = await user.validatePassword(password);
if (!passwordMatches) throw Error("Password does not match")
});
}
user.ts(用户猫鼬模型)
import mongoose from 'mongoose';
import bcrypt from 'bcrypt';
interface User {
username: string;
password: string;
isModified: (string: any) => boolean;
}
const SALT_WORK_FACTOR = 10;
const userSchema = new mongoose.Schema({
username: { type: String, required: true, index: { unique: true } },
password: {
type: String,
unique: false,
required: true
}
},
{ timestamps: true }
);
userSchema.pre('save', function(next) {
const user = this as unknown as User;
if (!user.isModified('password')) return next();
bcrypt.genSalt(SALT_WORK_FACTOR, (err: any, salt: any) => {
if (err) return next(err);
bcrypt.hash(user.password, salt, function(err: any, hash: any) {
if (err) return next(err);
user.password = hash;
next();
});
});
});
userSchema.methods.validatePassword = function (pass: string) {
return bcrypt.compare(pass, this.password);
};
const User = mongoose.model('User', userSchema);
export { User };