Sharad Chauhan
Sharad Chauhan

Reputation: 4891

Unable to use mongoose model

I am coding a simple registration form using mongoose. I have use a javascript file to process the values of the registration form.

Here is my registrationButtonAction.js

window.onload = function() {

    var User = require('/models/Mongoose Database/user_database');
    // this line is causing the problem


    var registerButton = document.getElementById("registerMe");
    var firstName = document.getElementById("firstName");
    var lastName = document.getElementById("lastName");
    var usernameRegister = document.getElementById("usernameRegister");
    var passwordRegister = document.getElementById("passwordRegister");
    var repasswordRegister = document.getElementById("repasswordRegister");


    registerButton.onclick = function () {

       if(!firstName.value || !passwordRegister.value || !repasswordRegister.value || !usernameRegister.value){
           alert("Enter all required fields");
       }else if (passwordRegister.value != repasswordRegister.value){
            alert("Passwords must match");
       }else {
           var newUser = new User({
                       username : usernameRegister.value,
                       password : passwordRegister.value
                   });
               User.find({username:usernameRegister.value}, function (error, user) {

                       if (error) throw error;

                       if(user){
                           window.location("/register");
                       }else {
                           newUser.save(function (error) {
                               if(error) throw error;
                           });
                           window.location("/login");
                       }
                       // user.comparePassword(passwordRegister.value, function (error, isMatch) {
                       //     if (error) throw error;
                       //
                       //     return 1;

                       //})
               });

       }

    }


}

When I comment the var User = require('/models/Mongoose Database/user_database');, all the checks are working fine inside the onclick function. But when I uncomment it, it is not recognizing the button click.

I want to know whether this is a correct way of taking values from the registration page and storing them in a mongoose database.

Upvotes: 2

Views: 99

Answers (1)

xShirase
xShirase

Reputation: 12389

You are mixing server and client code. Mongoose models and Node.js functions are not accessible inside window.onload on your client.

To put it simply, you need to create a REST API to perform database operations on the server. You have all the right tools aready, just need to reorder them.

The flow would be as such :

  • get the values entered in the browser
  • call an endpoint on your server (for example /api/createUser)
  • in the express router, have a route called /api/createUser in which you can access your User model and perform creation/deletion/update, etc.

My suggestion would be for you to go through this tutorial which should remove your confusion and bring you up to speed in no time. Good Luck!

Also, Passport can help you with authentication, but I believe you should first learn how to build a basic API. Authentication is a tricky beast ;)

Upvotes: 3

Related Questions