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 the server from router.`);
});
router.post('/register', (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 all the fields data.."});
}
User.findOne({ "$or": [ { email: email }, { phone: phone} ] })
.then((userExist) => {
if(userExist){
return res.status(422).json({error:"Email ID or Mobile No is already exist."});
}
const user = new User({name,email,phone,work,password,cpassword});
user.save().then(() => {
res.status(201).json({message:"User Registered Successfully."});
}).catch((err) => res.status(500).json({error:"Failed to regsiter user."}));
}).catch(err => { console.log(err)});
});
module.exports = router;
await User.findOne({ "$or": [ { email: email }, { phone: phone} ] });
This code worked for me. But if we want to differentiate both phone and email and aadhar number; so if email id is present than it will show it is present, and if phone no is present than it will show it is present, and if aadhar no is present than it will show. To do this how can we write the syntax?
You can always check userExist, which is the returned record from DB.
if(userExist.phone==phone)
err="Phone No exist"
else if (userExist.aadhar==aadhar)
err="Aadhar Exist"
Finally return the error string