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;
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} ] });