打字稿:将帖子请求正文转换为地图

Jér*_*hon 2 arrays post json node.js typescript

我正在使用节点 js 和打字稿编写一个 rest api,对于创建用户,我的 api 收到了一个 json 帖子:

import {Request, Response, Router} from "express";
import {User} from '../../../models/user.model';
import {createUser} from '../../../factories/user.factory';

export default [
        {
            path: "/api/v1/user/create",
            method: "post",
            handler: [
                async (req: Request, res: Response) => {
                    createUser(new User(req.body.user));
                    res.status(200).send(req.body);
                }
            ]
        }
    ];
Run Code Online (Sandbox Code Playgroud)

例如,我发送:

{
    "user": {
        "email": "test@gmail.com",
        "password": "12345678",
        "firstName": "Jérémy"
        }
    }
Run Code Online (Sandbox Code Playgroud)

我想用对象 req.body.user 创建一个对象“用户”:

import {Timestamp} from './timestamp.model';

export class User {
    id: bigint | undefined;
    email: string | undefined;
    password: string | undefined;
    firstName: string | undefined;
    lastName: string | undefined;
    pseudo: string | undefined;
    birthDate: Timestamp | undefined;
    lastEditDate: Timestamp | undefined;
    creationDate: Timestamp | undefined;
    googleAuthToken: string | undefined;
    language: string | undefined;
    profileAvatarUrl: string | undefined;
    profileBannerUrl: string | undefined;
    primaryLightColor: string | undefined;
    secondaryLightColor: string | undefined;
    primaryDarkColor: string | undefined;
    secondaryDarkColor: string | undefined;

    constructor(array: object) {
        console.log(array);
        // @ts-ignore
        console.log(array.gg);
        // @ts-ignore
        this.id = array.id;
        // @ts-ignore
        this.email = array.email;
        // @ts-ignore
        this.password = array.password;
        // @ts-ignore
        this.firstName = array.firstName;
        // @ts-ignore
        this.lastName = array.lastName;
        // @ts-ignore
        this.pseudo = array.pseudo;
        // @ts-ignore
        this.birthDate = array.birthDate;
        // @ts-ignore
        this.lastEditDate = array.lastEditDate;
        // @ts-ignore
        this.creationDate = array.creationDate;
        // @ts-ignore
        this.googleAuthToken = array.googleAuthToken;
        // @ts-ignore
        this.language = array.language;
        // @ts-ignore
        this.profileAvatarUrl = array.profileAvatarUrl;
        // @ts-ignore
        this.profileBannerUrl = array.profileBannerUrl;
        // @ts-ignore
        this.primaryLightColor = array.primaryLightColor;
        // @ts-ignore
        this.secondaryLightColor = array.secondaryLightColor;
        // @ts-ignore
        this.primaryDarkColor = array.primaryDarkColor;
        // @ts-ignore
        this.secondaryDarkColor = array.secondaryDarkColor;
        // @ts-ignore
    }

     toMap() {
        return {
            "id": this.id,
            "email": this.email,
            "firstName": this.firstName,
            "lastName": this.lastName,
            "pseudo": this.pseudo,
            "profileAvatarUrl": this.profileAvatarUrl,
            "birthDate": this.birthDate,
            "lastEditDate": this.lastEditDate,
            "creationDate": this.creationDate,
            "language": this.language,
            "googleAuthToken": this.googleAuthToken,
            "profileBannerUrl": this.profileBannerUrl,
            "primaryLightColor": this.primaryLightColor,
            "secondaryLightColor": this.secondaryLightColor,
            "primaryDarkColor": this.primaryDarkColor,
            "secondaryDarkColor": this.secondaryDarkColor,
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我已经把所有这些 "// @ts-ignore" 因为如果没有,我有这个错误:

src/models/user.model.ts(27,25): 错误 TS2339: 类型 'object' 上不存在属性 'id'。src/models/user.model.ts(28,32): 错误 TS2339: 类型“对象”上不存在属性“电子邮件”。src/models/user.model.ts(29,35): 错误 TS2339: 类型“对象”上不存在属性“密码”。src/models/user.model.ts(30,36): 错误 TS2339: 类型 'object' 上不存在属性 'firstName'。src/models/user.model.ts(31,35): 错误 TS2339: 属性 'lastName' 在类型 'object' 上不存在。

我的问题是:如何正确地让我的班级用户不必把所有这些“// @ts-ignore”?

提前致谢。杰瑞米。

Dan*_*inu 10

我有一个不同的建议,我觉得它在打字稿中很好,并开始大量使用。您可以将其定义为接口,而不是为您的用户创建一个类。

export interface User { 
 email: string, 
 password: string, 
 firstName: string,
 lastName: string, 
 // etc 
}
Run Code Online (Sandbox Code Playgroud)

然后简单地做:

const user = req.body.user as User; 
Run Code Online (Sandbox Code Playgroud)

只要您仅将这些用于创建没有业务逻辑的域模型对象,键入就会更快、更清晰。

编辑:

如果您需要坚持上课,请尝试使用任何 类型。

 export class user { 
 constructor(userDto: any) { 
   // your logic
   } 
 }


 new User(req.body.user); 
Run Code Online (Sandbox Code Playgroud)