I'm programming an rest api with node js and typescript and for create user, my api recieve a json post :
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);
}
]
}
];
For exemple, I send that :
{
"user": {
"email": "[email protected]",
"password": "12345678",
"firstName": "Jérémy"
}
}
I would like create an object "User" with the object 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,
}
}
}
I have put all this "// @ts-ignore" because if not , I've this error :
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'.
My question is : How correctly make my class user for not have to put all this "// @ts-ignore" ?
Thank's in advance. Jérémy.
array: object? Surely you're expecting it to be something else here?