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} ] });这个密码对我有用。但是,如果我们想要区分电话、电子邮件和aadhar号码,那么如果email id存在,它就会显示它是存在的,如果phone no存在,它就会显示它存在,如果aadhar no存在,它就会显示出来。要做到这一点,我们如何编写语法?
发布于 2021-12-26 07:10:12
您可以始终检查userExist,这是DB返回的记录。
if(userExist.phone==phone)
err="Phone No exist"
else if (userExist.aadhar==aadhar)
err="Aadhar Exist"最后返回错误字符串。
https://stackoverflow.com/questions/70484640
复制相似问题