0

I am working on a Node JS project where my app has the following folder structure:

src
│   index.js        # Entry point for application
└───config          # Application environment variables and secrets
└───controllers     # Express controllers for routes, respond to client requests, call services
└───interfaces      # Interfaces for Database models
└───middlewares     # Operations that check or maniuplate request prior to controller utilizing
└───models          # Database models
└───routes          # Express routes that define API structure
└───services        # Encapsulates all business logic

What the issue is whenever I am trying to invoke a method present in service file it's skipping the line after coming to it and going inside catch. Below is the code of my controller & service:

admin.controller.ts

import * as dotenv from 'dotenv';
import { Request, Response } from 'express';
import ERROR_TYPES from '../config/error';
import db from '../models';
import { AdminService } from '../services/admin.service';

export class AdminController {

    adminService = new AdminService();
    constructor() {
        dotenv.config();
    }

    async updateAdminUser(req: Request, res: Response) {
        try {
            console.log('a')
            let x = await this.adminService.updateAdminUserImpl(req.body, req.params.id); // By debugging I saw it's coming to this line and then going inside the catch block and not giving any error message
            console.log(x);
            res.status(ERROR_TYPES.UPDATED_SUCESSFULLY.code).send({ success: true, data: '', message: ERROR_TYPES.UPDATED_SUCESSFULLY.message, status: ERROR_TYPES.UPDATED_SUCESSFULLY.status });
        } catch(error) {
            res.status(ERROR_TYPES.SERVER_ERROR.code).send({ success: false, data: '', message: error, status: ERROR_TYPES.SERVER_ERROR.status });
        }
    }
}

admin.service.ts

import * as dotenv from 'dotenv';
import { Request } from 'express';
import { AdminUserI } from '../interfaces/admin_user.interface';
import db from '../models';

export class AdminService {

    constructor() {
        dotenv.config();
    }

    private Validator(data: AdminUserI): boolean {
        console.log(data);
        return false;
    }

    async updateAdminUserImpl(req: any, id: string) {
        console.log(req);
        if(this.Validator(req)) {
            try {
                await db.AdminUser.update(req, { where: { id: id } });
                return { success: true, data: '', message: 'xyz' };
            } catch(error) {
                return { success: true, data: '', message: error };
            }
        } else {
            return { success: true, data: '', message: 'def' };
        }
    }
}

I am not understanding from where it's stopping. I followed some blogs like this one but still can't figure it out.

2

1 Answer 1

1

You are passing the whole request to the function whose input type is declared as AdminUserI.

if(this.Validator(req))

and

private Validator(data: AdminUserI)
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not passing the whole request rather am passing the req.body only which is written in the controller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.