我正在尝试使用刷新令牌进行授权,并通过Express访问令牌。因此,我遇到了一个问题,在postman调用之后路径密码值是未定义的,尽管它记录在控制台中很好。
用户控制器:
class UserController {
async registration(req, res, next) {
try {
const { email, password } = req.body;
console.log("pass from user controller " + password);
const userData = await userService.registration(email, password);
res.cookie("refreshtoken", userData.refreshToken, {
maxAge: 30 * 24 * 60 * 60 * 1000,
httpOnly: true,
});
return res.json(userData);
} catch (error) {
console.log(error);
}
}
}
用户模型:
import mongoose from "mongoose";
const UserSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true,
},
password: {
type: String,
required: true,
},
isActivated: { type: Boolean, default: false },
activationLink: { type: String },
});
export default mongoose.model("User", UserSchema);
用户服务:
import UserModel from "../models/user-model.js";
import bcrypt from "bcrypt";
import { v4 as uuid } from "uuid";
import mailService from "./mail-service.js";
import tokenService from "./token-service.js";
import UserDto from "../dtos/user-dto.js";
class UserService {
async registration(email, password) {
const candidate = await UserModel.findOne({ email });
console.log("pass form user Service " + password);
if (candidate) {
throw new Error(`Usser with this email ${email} already exists`);
}
const hashPassword = await bcrypt.hash(password, 3);
const activationLink = uuid();
const user = await UserModel.create({
email,
hashPassword,
activationLink,
});
await mailService.sendActicvationMail(email, activationLink);
const userDto = new UserDto(user);
const tokens = tokenService.generateToken({ ...userDto });
await tokenService.saveToken(userDto.id, tokens.refreshToken);
return {
...tokens,
user: userDto,
};
}
}
export default new UserService();
我是和向导一起做的,所以也许我犯了一些我看不到的愚蠢的错误。
完全错误:
错误:{密码: ValidatorError: Path
password
是必需的。在验证(/home/galich/Desktop/backend/jwt-authorization/server/node_modules/mongoose/lib/schematype.js:1337:13) at SchemaString.SchemaType.doValidate (/home/galich/Desktop/backend/jwt-authorization/server/node_modules/mongoose/lib/schematype.js:1321:7) at /home/galich/Desktop/backend/jwt-authorization/server/node_modules/mongoose/lib/document.js:2831:18 at processTicksAndRejections (节点:内部/进程/任务队列:78:11){ properties: Object类别:‘必需’,路径:‘密码’,值:未定义,原因:未定义,符号(mongoose:validatorError):true },_message:‘用户验证失败’}
发布于 2022-08-23 12:19:38
您需要修改这个片段
const user = await UserModel.create({
email,
hashPassword,
activationLink,
});
至
const user = await UserModel.create({
email,
password: hashPassword,
activationLink,
});
https://stackoverflow.com/questions/73464634
复制相似问题