最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if email or phone already exists in mongodb database - Stack Overflow

programmeradmin0浏览0评论

I am facing a problem regarding checking of email and phone numbers in my MongoDB database. My code only checks if the email is present in the database but does not respond to the phone.

const express = require("express");
const router = express.Router();

require("../db/conn");
const User = require("../model/userSchema");

router.get("/", (req, res) => {
  res.send(`Hello World from server lolstar`);
});

router.post("/register", async (req, res) => {
  const { name, email, phone, work, password, cpassword } = req.body;

  if (!name || !email || !phone || !work || !password || !cpassword) {
    return res.status(422).json({ error: "Please fill your details" });
    }
    try {
        const userExist = await User.findOne({ email: email }, {phone: phone });
        if (userExist)
        {
             return res
          .status(422)
          .json({ error: "Email or Phone number already exists" });
        }
        
        const user = new User({ name, email, phone, work, password, cpassword });
        
        const userRegister = await user.save();
        if (userRegister)
        {
             res.status(201).json({ message: "User registered successfully" });
            }
      
    } catch (err) {
        console.log(err);
        
  }
  
});

module.exports = router;

I have added my UserSchema file. I don't think it has an error please check if something is wrong here. I want the code to check for both email and phone and then use it for authentication purpose.

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
    },
    email: {
        type: String,
        required: true,
    },
    phone: {
        type: Number,
        required: true,
    },
    work: {
        type: String,
        required: true,
    },
    password: {
        type: String,
        required: true,
    },
    cpassword: {
        type: String,
        required: true,
    }

})

const User = mongoose.model('USER', userSchema);

module.exports = User;

I am facing a problem regarding checking of email and phone numbers in my MongoDB database. My code only checks if the email is present in the database but does not respond to the phone.

const express = require("express");
const router = express.Router();

require("../db/conn");
const User = require("../model/userSchema");

router.get("/", (req, res) => {
  res.send(`Hello World from server lolstar`);
});

router.post("/register", async (req, res) => {
  const { name, email, phone, work, password, cpassword } = req.body;

  if (!name || !email || !phone || !work || !password || !cpassword) {
    return res.status(422).json({ error: "Please fill your details" });
    }
    try {
        const userExist = await User.findOne({ email: email }, {phone: phone });
        if (userExist)
        {
             return res
          .status(422)
          .json({ error: "Email or Phone number already exists" });
        }
        
        const user = new User({ name, email, phone, work, password, cpassword });
        
        const userRegister = await user.save();
        if (userRegister)
        {
             res.status(201).json({ message: "User registered successfully" });
            }
      
    } catch (err) {
        console.log(err);
        
  }
  
});

module.exports = router;

I have added my UserSchema file. I don't think it has an error please check if something is wrong here. I want the code to check for both email and phone and then use it for authentication purpose.

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
    },
    email: {
        type: String,
        required: true,
    },
    phone: {
        type: Number,
        required: true,
    },
    work: {
        type: String,
        required: true,
    },
    password: {
        type: String,
        required: true,
    },
    cpassword: {
        type: String,
        required: true,
    }

})

const User = mongoose.model('USER', userSchema);

module.exports = User;
Share Improve this question edited Aug 30, 2021 at 9:33 NeNaD 20.5k11 gold badges61 silver badges114 bronze badges asked Aug 30, 2021 at 9:13 Vivek JoshiVivek Joshi 711 silver badge6 bronze badges 2
  • Can you be more specific about your problem, more specific than "does not respond to the phone"? What is the expected behaviour, and what behaviour do you observe instead? – Heiko Theißen Commented Aug 31, 2021 at 6:12
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Aug 31, 2021 at 6:12
Add a ment  | 

1 Answer 1

Reset to default 5

You want to check if email OR phone exists in database. Your current query checks if both of them exist. You should use $or operator for your query, like this:

await User.findOne({ "$or": [ { email: email }, { phone: phone} ] });
发布评论

评论列表(0)

  1. 暂无评论