Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Looking for a way to destructure object and pass optional arguments to another method if they are exist

I want to make some properties optional and check whether propery exists and if it is I want to give it to this.UserService.update() method. I mean that i'm trying to update only those values which are passed to req.body.

user.controller.ts

private updateUser = async (
        req: Request,
        res: Response,
        next: NextFunction
    ): Promise<Response | void> => {
        try {
            const id = req.user.id;

            //I want to check every value below exist whether it exists
            const { old_password, name, email, password } = req.body;

            // Some of they may be empty and if some of them are not I want to give  
            // them to the method below
            const updatedUser = await this.UserService.update(id, /* here */);

            return res.status(200).json({ updatedUser });
        } catch (error: any) {
            next(new HttpException(400, error.message));
        }
    };

user.service.ts

public async update(
    id: string,
    old_password: string,
    name?: string,
    password?: string,
    email?: string
){}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can directly check in update function:


public async update(
    id: string,
    old_password: string,
    name?: string,
    password?: string,
    email?: string
){
   if(name) {
       // Update name
   }
   if(email) {
       // Update email
   }
   // ....
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!