I'm running into a strange problem with a very simple and straightforward JS code. Here, I'm trying to look up a given (email) value against a database and return a boolean true if a record with that value exists:
// utils/isEmailInDB.js
import User from '../models/user';
const isEmailInDB = (email) => {
let emailExists;
User.findOne({ emails: email }).then((existingUser) => {
if(existingUser) { emailExists = true; } else { emailExists = false; }
});
return emailExists;
};
export default isEmailInDB;
In another file, I'm calling this function and logging the returned value to the screen:
// routes/mail-routes.js
import express from 'express';
import sendEmail from '../utils/sendEmail';
import isEmailInDB from '../utils/isEmailInDB';
import { check, validationResult } from 'express-validator';
const router = express.Router();
router.post('/', [
check('to').isEmail(),
], (req, res) => {
const errors = validationResult(req);
const { to = '', pageURL = '/' } = req.body;
if (!errors.isEmpty()) {
res.redirect(pageURL + '?verify=1');
} else {
// Generate token
// Check if email exists in db
const existsInDB = isEmailInDB(to);
console.log('EMAIL EXISTS?', existsInDB);
sendEmail(to).then(() => {
res.redirect(pageURL + '?verify=2');
}).catch((err) => {
res.redirect(pageURL + '?verify=1');
});
}
});
module.exports = router;
With this line in the second snippet above: console.log('EMAIL EXISTS?', existsInDB);
, I expect to see one of the following outputs upon execution, depending on whether the email exists in DB:
EMAIL EXISTS? true
EMAIL EXISTS? false
However, no matter what I try, the line keeps returning undefined. Even when the email being looked up exists. What could I be doing wrong?