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

373
Views
¿Cómo verificar si un usuario existe en mongoose?

Entonces, estoy tratando de verificar si hay un usuario o no. Tengo el siguiente código:

 const userExist = await User.find({ username: req.body.username }); if (userExist) return res.status(400).send({ message: "User already exists" });

pero incluso si el usuario no existe, todavía da el error User already exists y el objeto userExists devuelve []

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

0

puedes usar const user = await User.findOne({ username: req.body.username }); esto obtiene solo uno y puedes verificar! usuario

about 4 years ago · Juan Pablo Isaza Report

0

La razón es [] es cierto en Javascript . Debe verificar la longitud y verificar si hay algunos datos en una matriz.

 const userExist = await User.find({ username: req.body.username }); if (userExist.length > 0) return res.status(400).send({ message: "User already exists" });

Para referencias futuras, todos los valores a continuación son verdaderos en JS.

 if (true) if ({}) if ([]) if (42) if ("0") if ("false") if (new Date()) if (-42) if (12n) if (3.14) if (-3.14) if (Infinity) if (-Infinity)

Nota: Le recomendamos que consulte este enlace.

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!