0

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"
}'

1 Answers1

1

The problem is with the validator. If you look closely it will be something like,

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;
            }
        });
    }
    return; // The invisible return 
}

When there is no error, what you'll get is the undefined from the invisible return. At the time when the promise is resolved, your function has already completed its execution.

Make the validator to return a promise in either case and handle it from the router.

const router = require('express').Router();
const UserV = require('./UserV');
    
router.get('/register',async(req,res) => {
      try {
          const count = await UserV(req.body);
          console.log(count);
       catch(error) {
           console.log(error); // error case is here
       }       
});

and have the validator similar to,

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 Promise.reject(error);
    }else{
        return User.countDocuments({email:res.email}).then(count => {
            if(count > 0){
                return 1;
            }else{
                return 0;
            }
        });
    }
}

theekshanawj
  • 485
  • 5
  • 9