Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda