验证器isStrongPassword不是一个函数



每当我尝试注册用户时,都会收到此错误!

isStrongPassword不是函数

我试着键入isStrongPassword,它至少有8个字符,1个大写,1个小写,1个数字,1个符号,但运气不好,我不知道我缺少了什么!!

Auth.js

const isEmail = require ("validator/lib/isEmail.js")
const isStrongPassword = ("validator/lib/isStrongPassword");
//REGISTER
router.post("/register", async (req, res, next) => {
try {
if (!req.body.username) {
return next(createError(400, "Invalid username"));
} if (!req.body.email || !isEmail(req.body.email)) {
return next(createError(400, "Invalid email"));
} if (!req.body.password) {
return next(createError(400, "Invalid password"));
} else if (!isStrongPassword(req.body.password)) {
return next(
createError(
400,
"Invalid password: Min 8 characters, 1 uppercase, 1 lowercase, 1 number, 1 symbol"
)
);
} else {
const checkUsername = await User.findOne({ username: req.body.username });
const checkEmail = await User.findOne({ email: req.body.email });
if (checkUsername) {
return next(createError(400, "Username taken"));
} else if (checkEmail) {
return next(createError(400, "Email taken"));
} else {
//generate new password
const salt = await bcrypt.genSalt(10);
const hashedPass = await bcrypt.hash(req.body.password, salt);
//create new user
const newUser = new User ({
username: req.body.username,
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: hashedPass,
repeatPassword: hashedPass,
birthday: req.body.birthday,
});
//save user and respond
const user = await newUser.save();
res.status(200).json({
message: "Successfully registered",
user,
});
}
}

} catch(err) {
next(err);

}
});

isStrongPassword导入中缺少一个require语句,可能还缺少文件扩展名。例如

const isStrongPassword = ("validator/lib/isStrongPassword");

应该是

const isStrongPassword = require ("validator/lib/isStrongPassword.js");

相关内容

  • 没有找到相关文章

最新更新