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

169
Views
Promesa {<pendiente>} de bcrypt dentro de la promesa

Tengo una función lambda que está conectada a Mongo a través de mangosta. El usuario ingresa un nombre y una contraseña en la interfaz y la función lambda busca si el usuario existe y luego la contraseña cifrada se compara a través de bcrypt.

En este momento, bcrypt me da {<promise pending>} , cuando consola.log (coincidencia).

Sin embargo, no puedo simplemente agregar una espera ya que el cheque está dentro de una promesa.

 require("dotenv").config(); const mongoose = require("mongoose"); const User = require('../server/models/user'); const bcrypt = require('bcryptjs') exports.handler = async (event, context) => { context.callbackWaitsForEmptyEventLoop = false; mongoose.connect(`${process.env.MONGO_URI}`,{ useNewUrlParser: true, useUnifiedTopology: true, } ); let {name, password} = JSON.parse(event.body); let fd = await new Promise((res,rej)=>User.findOne( { name:name, }, (err, user) => { if (err) { rej({ statusCode: 500, body: JSON.stringify({ msg: err.message }), }); } else { //==> const match = await bcrypt.compare(password, JSON.stringify(user.password)); if (match) { let statusCode = user && match? 200:405 res({ statusCode, headers: { "Access-Control-Allow-Origin": "*", "Content-Type": "application/json", }, body: JSON.stringify({ user}), }); } } } )); return fd };

Entonces, básicamente, ¿cómo puedo 'esperar' bcrypt en ese caso o cómo podría escribir esta función de otra manera? ¡Gracias por leer!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

No puede usar await allí, porque la devolución de llamada en la que ocurre no es una función async .

Sin embargo, debería usar mejor la promesa que puede obtener de findOne :

 exports.handler = async (event, context) => { context.callbackWaitsForEmptyEventLoop = false; mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true, }); const {name, password} = JSON.parse(event.body); const user = await User.findOne({name}).exec(); const match = user && await bcrypt.compare(password, user.password.toString()); if (!match) { throw { statusCode: 405, body: "Invalid user or password", } } return { statusCode: 200, headers: { "Access-Control-Allow-Origin": "*", "Content-Type": "application/json", }, body: JSON.stringify({ user}), }; };
about 4 years ago · Juan Pablo Isaza Report

0

podrías encadenar la función dentro de la promesa:

 let fd = await new Promise((res, rej) => User.findOne( { name: name, }, (err, user) => { if (err) { rej({ statusCode: 500, body: JSON.stringify({ msg: err.message }), }); } else { bcrypt .compare(password, JSON.stringify(user.password)) .then((match) => { if (match) { let statusCode = user && match ? 200 : 405; res({ statusCode, headers: { "Access-Control-Allow-Origin": "*", "Content-Type": "application/json", }, body: JSON.stringify({ user }), }); } }); } } ) );
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!