I have created a validator to verify the down streaming data to server and also checked whether the data is exits in database
model.js
const mongoose = require('mongoose');
const User = new mongoose.Schema({
email:{
type:String,
unique:true,
required:true,
},
password:{
type:String,
required:true,
}
},{timestamps:true});
module.exports = mongoose.model('bbusers',User);
And A validator
const joi = require('joi');
const User = require('./model');
const UserModel = joi.object({
email:joi.string().email(),
password:joi.string().min(5)
});
module.exports = (res) => {
const {error, value} = UserModel.validate(res);
if(error){
return error;
}else{
User.countDocuments({email:res.email}).then(count => {
if(count > 0){
return 1;
}else{
return 0;
}
});
}
}
And a Last a router for getting invoking the function
const router = require('express').Router();
const UserV = require('./UserV');
router.get('/register',async(req,res) => {
console.log(UserV(req.body))
});
.. When Ever I am making a query with a unstructured email value I got an error which is as excepted but Whenever I am making a structured email to check whether the email is there in database instead of getting 1 or 0 I get undefined
CURL ----
curl --location --request GET 'localhost:3000/register' \
--header 'Content-Type: application/json' \
--data-raw '{
"email":"guptaanish450gmail.com",
"password":"adfdsdff"
}'