I'm attempting to set up a dynamic registration page that checks if an account exists by getting the e-mail value on keyUp and checking it against a MongoDB via Mongoose.
Here is the Pug page:
html
include head.pug
script
include ../register.js
include navbar.pug
body
form(action = "/register", method = "POST")
div.form
p#pnotice
label(for = "firstname") First Name
br
input(name = "firstname")
br
br
label(for = "lastname") Last Name
br
input(name = "lastname")
br
br
label(for = "email") E-Mail
br
input#email(name = "email")
br
br
label(for = "password") Password
br
input#pass1(name = "password" type = "password")
br
br
label(for = "passwordc") Password Confirmation
br
input#pass2(name = "passwordc" type = "password")
br
br
button(type = "submit") Register
Then here is the registration code, with the password check function running fine, but the User.findOne() function not working at all:
$(document).ready(function() {
$('#pass2').keyup(function() {
let p1 = $('#pass1').val();
let p2 = $('#pass2').val();
if (p1 !== p2) {
$('#pnotice').show();
$('#pnotice').text('Password Must Match');
} else {
$('#pnotice').hide();
}
});
$('#email').keyup(function() {
//alert($('#email').val());
let query = $('#email').val();
User.findOne({email: query}, function(err, res) {
if (res) {
console.log("FOUND");
} else {
console.log("NOT FOUND");
}
});
})
});
And here's the relevant code from my Express:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_db');
var userSchema = mongoose.Schema({
firstname: String,
lastname: String,
email: String,
password: {
type: String,
required: true,
bcrypt: true
},
teacher: String,
time: Number
});
var User = mongoose.model("User", userSchema);
I'm really new to express and all this, so I imagine I'm making a rookie mistake, but I'm assuming the User variable is out of scope in the register.js file since it is a separate file. Is there a way to pass the variable around or give it a larger scope so it can be accessed by all files? Or am I doing this completely incorrectly?