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

248
Views
How can I check if a user already exist in mongoDB?

I am creating an account creation system with express.js but I would like to know how I can find out if the user already has an account (with his email) on my DB in order not to register him twice.

const router = require("express").Router(),
    User = require("../database/models/User.model.js");

const cryptoJS = require("crypto-js");

//REGISTER
router.post("/register", async (req, res) => {
    const { username, email, password } = req.body;

    if (User.findOne({ email: email })) {
        return res.status(400).json({
            message: "This email is alredy existing in our database" 
       });
    } else {
        const user = new User({
            username: username,
            email: email,
            password: cryptoJS.AES.encrypt(
                password,
                process.env.CRYPTO_KEY
            )
        });
        await user.save()
            .catch(err => res.status(500).json({ error: err.message }));
    }
});

module.exports = router;

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

0

const router = require("express").Router(),
const User=require("../database/models/User.model.js");

const cryptoJS = require("crypto-js");

//REGISTER
router.post("/register", async (req, res) => {
    const { username, email, password } = req.body;
    const checkUser = await User.findOne({ email: email })

    if (checkUser) {
        return res.status(400).json({
            message: "This email is alredy existing in our database" 
       });
    } else {
        const user = new User({
            username: username,
            email: email,
            password: cryptoJS.AES.encrypt(
                password,
                process.env.CRYPTO_KEY
            )
        });
        await user.save()
            .then(data=>res.json(data))
            .catch(err => res.status(500).json({ error: err.message }));
    }
});

module.exports = router;
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!