Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
saurav-commits opened this issue Nov 23, 2023 · 0 comments

Comments

@saurav-commits
Copy link

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 };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant