2

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?

Arcaster
  • 119
  • 8

1 Answers1

1

You are trying to execute your server side nodejs code from client side JavaScript. Unfortunately you can't do that directly. You will need to set up some of REST API or similar to call the express code.

You could have your client call an endpoint such as /user?email=myemail@company.com

Then have a REST service listen for calls to GET /user then that is where you do the User.findOne() and respond with the result

andy mccullough
  • 9,070
  • 6
  • 32
  • 55
  • Alright, this makes sense to me. I'm still wrapping my head around what's server-side and what's client-side and the limitations. Can I assume everything in my express file is strictly server-side code? – Arcaster Apr 14 '19 at 19:59
  • Yep, express is server side. Collate user input / data on the cliet side and use HTTP to send/receive data to and from the server side API – andy mccullough Apr 14 '19 at 20:02
  • Have a look online for 'Node Rest API with Mongoose' for examples, plenty out there – andy mccullough Apr 14 '19 at 20:03