【发布时间】:2019-09-20 02:10:44
【问题描述】:
我正在用 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 创建一个对象“用户”:
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): 错误 TS2339: 属性 '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:“对象”类型上不存在属性“名字”。 src/models/user.model.ts(31,35):错误 TS2339:属性“lastName” 类型“对象”上不存在。
我的问题是:如何正确地让我的班级用户不必输入所有这些“// @ts-ignore”?
提前致谢。 杰里米。
【问题讨论】:
-
为什么指定
array: object?您肯定希望这里有别的东西吗? -
我已经使用 Array
和 Map 进行了测试,但是我无法使用 ts_ignore 访问我的 values 事件。我已将其添加到我的代码中:console.log(typeof req.body.user);还有那个打印对象。
标签: arrays node.js json typescript post