Quiero hacer que algunas propiedades sean opcionales y verificar si existe la propiedad y, si es así, quiero dársela al método this.UserService.update(). Quiero decir que estoy tratando de actualizar solo los valores que se pasan a req.body.
usuario.controlador.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)); } };usuario.servicio.ts
public async update( id: string, old_password: string, name?: string, password?: string, email?: string ){}Puede comprobar directamente en la función de actualización:
public async update( id: string, old_password: string, name?: string, password?: string, email?: string ){ if(name) { // Update name } if(email) { // Update email } // .... }