Я программирую остальные API с помощью узла js и машинописного текста, и для создания пользователя мой 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:
Свойство 'email' не существует для типа 'object'.
src / models / user.model.ts (29,35): ошибка TS2339: свойство 'пароль'
не существует для типа «объект». SRC / модели / user.model.ts (30,36):
Ошибка TS2339: Свойство 'firstName' не существует для типа 'объект'.
src / models / user.model.ts (31,35): ошибка TS2339: свойство 'lastName'
не существует для типа «объект».
У меня такой вопрос: как правильно заставить моего ученика не добавлять все это "// @ ts-ignore"?
Спасибо заранее.
Жереми.