gpt4 book ai didi

arrays - typescript : convert post request body to map

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:10 24 4
gpt4 key购买 nike

我正在使用 Node js 和 TypeScript 编写一个 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);
}
]
}
];

例如,我发送:

{
"user": {
"email": "test@gmail.com",
"password": "12345678",
"firstName": "Jérémy"
}
}

我想使用对象 req.body.user 创建一个对象“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,
}
}

}

我已经把所有这些“//@ts-ignore”都放在了,因为如果没有,我就会遇到这个错误:

src/models/user.model.ts(27,25): error TS2339: Property 'id' does not exist on type 'object'. src/models/user.model.ts(28,32): error TS2339: Property 'email' does not exist on type 'object'. src/models/user.model.ts(29,35): error TS2339: Property 'password' does not exist on type 'object'. src/models/user.model.ts(30,36): error TS2339: Property 'firstName' does not exist on type 'object'. src/models/user.model.ts(31,35): error TS2339: Property 'lastName' does not exist on type 'object'.

我的问题是:如何正确地让我的类(class)用户不必输入所有这些“//@ts-ignore”?

提前致谢。杰里米。

最佳答案

我有一个不同的建议,我觉得 typescript 很好,并开始大量使用。您可以将其定义为接口(interface),而不是为用户创建类。

export interface User { 
email: string,
password: string,
firstName: string,
lastName: string,
// etc
}

然后只需执行:

const user = req.body.user as User; 

只要您仅使用它们来创建没有业务逻辑的域模型对象,输入就会更快、更干净。

编辑:

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

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


new User(req.body.user);

关于arrays - typescript : convert post request body to map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55937458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com