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

261
Visualizações
How to do avoid huge nested if else

This is my code for logging in

method: 'POST',
        path: '/api/login/sp',
        config: { auth: false },
        handler: function (request, reply) {
            User.findOne({ phone: request.payload.phone }, function (err, user) {
                if (err) throw err;
                if (user !== null) {
                    user.comparePassword(request.payload.password, function (err, isMatch) {
                        if (err) throw err;
                        if (isMatch) { // Login success
                            data = {
                                "statusCode": 200,
                                "token": generateJWT(user._id)
                            }
                            return reply(data);
                        }
                        else {
                            reply(Boom.unauthorized('Invalid Account'))
                        }
                    });
                }
                else { // Invalid User
                    reply(Boom.unauthorized('Invalid Account'))
                }
            });
        }

It takes a lot of code and makes it very hard to read. Is there a way to better write this part of the code so that it is easily maintainable and readable?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You may use return reply():

User.findOne({phone: request.payload.phone}, function (err, user) {
    if (err) throw err;
    if (user === null) return reply(Boom.unauthorized('Invalid Account'));
    user.comparePassword(request.payload.password, function (err, isMatch) {
        if (err) throw err;
        if (!isMatch) return reply(Boom.unauthorized('Invalid Account'));
        data = {
            "statusCode": 200,
            "token": generateJWT(user._id)
        };
        return reply(data);
    });
})
over 4 years ago · Santiago Trujillo Relatório

0

Try using the return early pattern: Return early pattern for functions

User.findOne(..., {
    // generic error
    if (err) throw err;

    // invalid user
    if (user === null) {
        reply(Boom.unauthorized('Invalid Account'));
        return;
    }

    user.comparePassword(..., {
        if (err) throw err;

        if (!isMatch) {
            reply(Boom.unauthorized('Invalid Account'));
            return;
        }

        data = {
            "statusCode": 200,
            "token": generateJWT(user._id)
        };
        reply(data);
    });
});
over 4 years ago · Santiago Trujillo 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