typestack / typeorm-typedi-extensions

Dependency injection and service container integration with TypeORM using TypeDI library.
MIT License
262 stars 37 forks source link

what am i doing wrong here? i have used useContainer in config class. #68

Open saurav-commits opened 11 months ago

saurav-commits commented 11 months ago

for "fieldSetRepo" i get undefined help me with resolve it

import 'reflect-metadata'; import { Request } from 'express'; import Container, { Service, Inject } from 'typedi'; import { Repository, getRepository } from 'typeorm'; import { IResponse } from '@interfaces'; import { success } from '@utils/common.util'; import { FieldSettings } from '@models/field-settings.model'; import { InjectRepository } from 'typeorm-typedi-extensions';

@Service() class FieldSettingsController {

@InjectRepository(FieldSettings)
private fieldSetRepo: Repository<FieldSettings>;

private constructor() {
    // Private constructor to enforce singleton pattern
}

static getInstance(): FieldSettingsController {
    if (!FieldSettingsController.instance) {
        FieldSettingsController.instance = new FieldSettingsController();
    }
    return FieldSettingsController.instance;
}

private static instance: FieldSettingsController;

createFields = async (req: Request, res: IResponse): Promise<void> => {
    try {
        const fields = new FieldSettings({
            name: req.body.name
        });

        if (!this.fieldSetRepo) {
            throw new Error('Repository not initialized');
        }

        await this.fieldSetRepo.save(fields);

        res.status(200).json(success('', {}, res.statusCode));
    } catch (err) {
        console.error('Error:', err.message);
        res.status(400).json(success('', {}, res.statusCode));
    }
};

getAllFields =  async (req: Request, res: IResponse): Promise<void> => {
            try {
                const fields = await this.fieldSetRepo.find();

                res.status(200).json(success('', {fields}, res.statusCode));
            } catch (err) {
                console.log('err', err);
                res.status(400).json(success('', {}, res.statusCode));
            }
        }
// ... other methods ...

}

const fieldSettingsController = FieldSettingsController.getInstance(); export { fieldSettingsController as FieldSettingsController };