Я пытаюсь сделать POST запрос и получаю эту ошибку:
{
"msg": "Cannot read properties of undefined (reading 'securityService')"
}
Кроме того, когда я пытаюсь выполнить console.log(this.securityService), и получаю такую же ошибку.
security.controller:
/* eslint-disable no-console */
import { IRouter, Request, Response } from 'express';
import { StatusCodes } from 'http-status-codes';
import SecurityService from './security.service';
class SecurityController {
private securityService: SecurityService;
constructor(private router: IRouter) {
this.router = router;
this.securityService = new SecurityService();
}
public async sendVerificationCode(req: Request, res: Response) {
try {
console.log(this.securityService);
const { receiver } = req.query;
const user = await this.securityService.getUser(String(receiver));
console.log(user);
if (!user) {
return res.status(StatusCodes.CONFLICT).json({ msg: "User with this phone number doesn't exist" });
}
const id = await this.securityService.sendSms;
return res.status(StatusCodes.OK).json({ id });
} catch (error) {
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ msg: error.message });
}
}
}
export default SecurityController;
security.service:
/* eslint-disable no-console */
import { getRepository } from 'typeorm';
import VerificationEntity from '../entities/verification.entity';
class SecurityService {
public async getUser(receiver: string) {
console.log('Check 2');
return getRepository(VerificationEntity).findOne({ mobilePhone: receiver });
}
public async sendSms(body: { receiver: string }) {
const verificationCode = process.env.VERIFICATION_CODE;
await getRepository(VerificationEntity).insert({ mobilePhone: body.receiver, verificationCode });
const { id } = await getRepository(VerificationEntity).findOne();
console.log('Check 3');
return getRepository(VerificationEntity).findOne({ id: id as string });
}
}
export default SecurityService;