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

262
Views
Cómo evitar un anidado enorme si no

Este es mi código para iniciar sesión.

 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')) } }); }

Requiere mucho código y lo hace muy difícil de leer. ¿Hay alguna manera de escribir mejor esta parte del código para que sea fácil de mantener y leer?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede usar 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 Report

0

Intente usar el patrón de retorno temprano: patrón de retorno temprano para funciones

 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 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!