im trying to use this function convertError to convert error from zod, but i always get that return: TypeError: Cannot read property 'formatError' of undefined
import { IMiddlewaresRules } from "../IMiddlewaresRules";
import { Request, Response, NextFunction } from "express";
import { figureBodySchema } from "../zodschemas/schemasZod";
import { ZodError } from "zod";
class FormatError {
convertError(
err: ZodError
): Array<{ message: string; key: (string | number)[]; code: number }> {
return err.issues.map((err) => {
const error = {
message: err.message,
key: err.path,
code: 400,
};
return error;
});
}
}
export class VerifyRequestsBody implements IMiddlewaresRules {
constructor(private formatError: FormatError) {}
verifyBodyFigure(
req: Request,
res: Response,
next: NextFunction
): Response | any {
const { data } = req.body;
try {
figureBodySchema.parse(data);
} catch (err) {
if (err instanceof ZodError) {
return res.status(400).json(this.formatError.convertError(err));
}
}
next();
}
}
i don't understand why this happen, i already define the function before
full code here: https://github.com/DanielTrybe/backend-figures/tree/1.2.0-middleware