Nodejs + Typescript如何使用类模型?

问题描述 投票:0回答:1

我正在尝试学习如何在Node.js中使用类模型,我将为您提供我要实现的目标的示例。

例如,我有user.ts模型

export default class User {

    private firstname: string;
    private lastname: string;
    private email: string;
    private password: string;
    private isVerified: boolean;

    getFirstName() {
        return this.firstname;
    }

    setFirstName(val: string) {
        this.firstname = val;
    }

    getLastName() {
        return this.lastname;
    }

    setLastName(val: string) {
        this.lastname = val;
    }

    getEmail() {
        return this.email;
    }

    setEmail(val: string) {
        this.email = val;
    }

    getPassword() {
        return this.password;
    }

    setPassword(val: string) {
        this.password = val;
    }
}

而且我有auth.ts路线:

router.post('/login', async (req: Request, res: Response) => {
    const { error } = loginValidation(req.body);
    if (error) return res.status(400).send(error.details);

    const user = await UserModel.findOne({ email: req.body.email });
    if (!user) return res.status(400).send('Email doesn`t exist');
    if (!user.isVerified) return res.status(400).send('User is not verified, check Your email first');

    const validPass = await bcrypt.compare(req.body.password, user.password);
    if(!validPass) return res.status(400).send('Invalid password');
        const userResponse = new UserModel({
            firstname: user.firstname,
            lastname: user.lastname,
            email: user.email,
            createdAt: user.createdAt,
            updatedAt: user.updatedAt,
            isVerified: req.body.isVerified
        });

        try {
            const token = jwt.sign({_id: user._id}, process.env.TOKEN_SECRET,{ expiresIn: '8h' });
            res.header('auth-token', token);
            res.send(userResponse);
        } catch (err) {
            res.status(400).send(err);
        }
    }

我想将我的user.model用于req.body,但不知道如何:

已尝试:

常量用户:User = req.body;

user.getFirstname()-getFirstname()不是函数

user.setFirstname(req.body.firstname)

我如何将请求正文附加到用户模型?

node.js typescript
1个回答
0
投票

req.body返回类型未知的JSON对象。您需要类型保护来检查并确保req.body包含所需的值。您可以在这里https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types了解更多信息。

但是首先,您正在尝试在打字稿中实现吸气剂和吸气剂,但是这样做的方式是错误的。

所以我重新编写了您的User模型类

export default class User {

    private _firstname: string;
    private _lastname: string;
    private _email: string;
    private _password: string;
    private _isVerified: boolean;

    get firstname() {
        return this._firstname;
    }

    set firstname(val: string) {
        this._firstname = val;
    }

    get lastname() {
        return this._lastname;
    }

    set lastname(val: string) {
        this._lastname = val;
    }

    get email() {
        return this._email;
    }

    set email(val: string) {
        this._email = val;
    }

    get password() {
        return this._password;
    }

    set password(val: string) {
        this._password = val;
    }
}

和您的auth.ts路线

router.post('/login', async (req: Request, res: Response) => {
    if((req.body as User).firstname){
        const user: User = req.body as User
        ...
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.