Cannot put a require module inside of a handlebars script

I’m having a trouble on trying to figure out how to obtain this:

E_money value: 1000 → save it to database(this is already done)

Now when i click a button it should deduct a 100 value so it should be 900 and save it to database.(Having trouble here).

my Structure folder look like this:

models->user.js

routes->index.js and users.js

and my handlebars

views->layouts->layout.handlebars

here is my code so far in models->user.js

    var mongoose = require('mongoose');
    var bcrypt = require('bcryptjs');

    //User Schema
    var UserSchema = mongoose.Schema({
    username:{
    type: String,
    index:true
    },
    password:{
    type:String
    },
    email:{
    type:String
    },
    name:{
    type:String
    },
    field:{
    type:String
    },
    e_money:{
    type:Number //this is the integer form in mongoose
    }
    });

    //accesible variable from the outside
    var User = module.exports = mongoose.model('User', UserSchema);

    //create the user
    module.exports.createUser= function(newUser, callback){
    bcrypt.genSalt(10, function(err,salt){
    bcrypt.hash(newUser.password, salt, function(err, hash){
    //store hash in your password DB
    newUser.password = hash;
    newUser.save(callback);
    });
    });
    }



    module.exports.getUserByUsername = function(username, callback){
    var query = {username: username};
    User.findOne(query, callback);
    }

    module.exports.getUserById = function(id, callback){
    User.findById(id, callback);
    }

    module.exports.comparePassword = function(candidatePassword, hash, callback){
    bcrypt.compare(candidatePassword, hash, function(err, isMatch){
    if(err) throw err;
    callback(null, isMatch);
    });
    }

On my routes->users.js here is my code:

    //To run the application
    var express = require('express');
    var router = express.Router();
    var passport = require('passport');
    var LocalStrategy = require('passport-local').Strategy;

    var User = require('../models/user');

    //Register
    router.get('/register', function(req,res){
    res.render('register');
    });

    //Login
    router.get('/login',function(req,res){
    res.render('login');
    });

    //Register User
    router.post('/register', function(req,res){
    var name = req.body.name;
    var email = req.body.email;
    var username = req.body.username;
    var password = req.body.password;
    var password2 = req.body.password2;
    var field = req.body.field;

    //temporary for emoney
    var e_money = req.body.e_money;

    //show what's been written in web to console(name)
    //console.log(name);

    //validation - Check to see if the field is empty
    req.checkBody('name', 'Name is required!').notEmpty();
    req.checkBody('email', 'Email is required!').notEmpty();
    req.checkBody('email', 'Email is not valid!').isEmail();
    req.checkBody('username', 'Username is required!').notEmpty();
    req.checkBody('password', 'Password is required!').notEmpty();
    req.checkBody('password2', 'Password does not match').equals(req.body.password);
    req.checkBody('field', 'Please specify if you are a Teacher or a Student!').notEmpty();

    //temporary for emoney
    req.checkBody('e_money','Please add some value in this field').notEmpty();

    var errors = req.validationErrors();

    if(errors){
    res.render('register',{
        errors:errors
    });
    }else{
        //new user in the model(user.js)
    var newUser = new User({
        name: name,
        email: email,
        username: username,
        password: password,
        field: field,
        e_money: e_money
    });

    User.createUser(newUser,function(err, user){
        if(err) throw err;
        console.log(user);
    });
    req.flash('success_msg', 'You are registed and can now login');

    res.redirect('/users/login');
    }
    });

    passport.use(new LocalStrategy(
    function(username, password, done){
    User.getUserByUsername(username, function(err, user){
    if(err) throw err;
    if(!user){
        return done(null, false, {message: 'Unknown User'});
    }

    User.comparePassword(password, user.password, function(err, isMatch){
        if(err) throw err;
        if(isMatch){
            return done(null, user);
        }
        else{
            return done(null, false, {message: "Invalid password"});
        }
    });
    });
    }));

    passport.serializeUser(function(user, done){
    done(null, user.id);
    });

    passport.deserializeUser(function(id, done){
    User.getUserById(id, function(err, user){
    done(err,user);
    });
    });

    router.post('/login',
    passport.authenticate('local',{sucessRedirect:'/',failureRedirect:'/users/login',failureFlash: true}),
    function(req,res){
    //dashboard
    if (req.user.field == "student") {
        req.flash('stud_val', 'student');
    }else if(req.user.field == "teacher"){
        req.flash('teach_val', 'teacher');
    }else if (req.user.field == "admin") {
        req.flash('admin_val', 'teacher');
    }
    res.redirect('/');
    });

    router.get('/logout',function(req, res){
    req.logout();
    req.flash('success_msg', 'You are logged out');

    res.redirect('/users/login');
    })

    module.exports = router;

And on my layout.handlebars here it is:

    <!DOCTYPE html>
    <html>
    <head>
    {{#if user}}
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <title>PEKTOS | Live Stream</title>
    <!-- Favicon-->
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin,cyrillic-ext" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" type="text/css">
    <!-- Bootstrap Core Css -->
    <link href="plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
    <!-- Waves Effect Css -->
    <link href="plugins/node-waves/waves.css" rel="stylesheet" />
    <!-- Animation Css -->
    <link href="plugins/animate-css/animate.css" rel="stylesheet" />
    <!-- Morris Chart Css-->
    <link href="plugins/morrisjs/morris.css" rel="stylesheet" />
    <!-- Custom Css -->
    <link href="css/style.css" rel="stylesheet">
    <!-- AdminBSB Themes. You can choose a theme from css/themes instead of get all themes -->
    <link href="css/themes/all-themes.css" rel="stylesheet" />

    <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script type="text/javascript" src="/easyrtc/easyrtc.js"></script>
    <script type="text/javascript" src="/easyrtc/labs/easyrtc_recorder.js"></script>
    {{#if stud_val}}
    <script type="text/javascript" src="js/demo_multistream_stud.js"></script>
    {{/if}}
    {{#if teach_val}}
    <script type="text/javascript" src="/easyrtc/labs/desktop_capture_iframe_version.js"></script>
    <script type="text/javascript" src="js/demo_multistream.js"></script>
    {{/if}}
    <!-- <script type="text/javascript" src="js/demo_instant_messaging_rooms.js"></script> -->

    <!-- for button -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js">    </script>
    <script type="text/javascript" src="prettify/prettify.js"></script>
    <script type="text/javascript" src="js/jquery.slimscroll.js"></script>



    {{else}}
  <meta charset="UTF-8">
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <title>Pektos | Log in</title>
  <!-- Favicon-->
  <link rel="icon" href="/favicon.ico" type="image/x-icon">
  <!-- Google Fonts -->
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin,cyrillic-ext" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" type="text/css">

    <!-- Bootstrap Select Css -->
    <link href="plugins/bootstrap-select/css/bootstrap-select.css" rel="stylesheet" />

  <!-- Bootstrap Core Css -->
  <link href="/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">

  <!-- Waves Effect Css -->
  <link href="/plugins/node-waves/waves.css" rel="stylesheet" />

  <!-- Animation Css -->
  <link href="/plugins/animate-css/animate.css" rel="stylesheet" />

  <!-- Custom Css -->
  <link href="/css/style.css" rel="stylesheet">

    {{/if}}
    </head>
    <!--Load view-->
    {{#if user}}
    <body class="theme-red" style="overflow: hidden;">
    {{{body}}}
    {{else}}
    <body class="login-page">
    <div class="login-box">
    <div class="logo">
        <a href="javascript:void(0);">Pek<b>Tos</b></a>
        <small>Right On Target</small>
    </div>
    <div class="card">
        <div class="body">
            <form id="sign_in" method="POST">
                <div class="msg">
                  {{#if success_msg}} <!--Global variable that has been set in app.js-->
                  <div class="alert alert-success">{{success_msg}}
                  </div>
                  {{/if}}
                  {{#if error_msg}}
                  <div class="alert alert-danger">{{error_msg}}
                  </div>
                  {{/if}}
                  {{#if error}}
                  <div class="alert alert-danger">{{error}}
                  </div>
                  {{/if}}
                  {{{body}}}
                </div>
            </form>
        </div>
    </div>
    </div>
    {{/if}}
    {{#if user}}
    <script>

    $('#localVideos').click(function(){
        $('nav').toggle('drop',{direction: 'up'}, 500);
        $('.btnMenuFloat').toggle('drop',{direction: 'left'}, 500);
        $('.actionBtnFloat').toggle('drop',{direction: 'down'}, 500);
        $('.btnfloat').toggle('drop',{direction: 'down'}, 500);
        $('.actionPanel2').hide();
    });

    function muteMe(id) {
        if($('.cv'+id).prop('muted')){
             $('.cv'+id).prop('muted', false);
             var child = document.getElementById("micid"+id);
             child.innerHTML = "mic";
             $('.micbut'+id).css("color", "green");
        }
        else{
             $('.cv'+id).prop('muted', true);
             var child = document.getElementById("micid"+id);
             child.innerHTML = "mic_off";
             $('.micbut'+id).css("color", "red");
        }
    }

    function showMe(id) {
        $('#span'+id).toggle("drop",{direction: 'right'},500);
    }

    $(document).ready(function(){
        var socket = io();

            //understand button
        $(".understandbtn").click(function(){
            //reset the timer every 3 second of interval
            $('.actionBtnFloat').css('z-index','0');

**//e_money**

    var deduction = 100;

    var newMoney = {{user.e_money}} - deduction;

    alert("It is working" + newMoney);

**//end**

**The only problem on the above e_money code is that i couldn't save it to my database using mongoose.**

            clearTimeout(interval);
            //send the data to the server
            socket.emit('chat message', getUser());
            var interval = setTimeout(function(){
                $('.'+getUser()).fadeIn();
            },5000);
        });

        socket.on('chat message', function(msg){
            $('.cv'+msg).fadeOut();
            $('.'+msg).append('<img id="bulb" class="actImage" src="images/understand button.png" width="50" height="50">');
            $('#say'+msg).html('I understand');
            var intervals = setTimeout(function(){
                $('#say'+msg).html('');
                $('#bulb').remove();
                $('.cv'+msg).fadeIn();
            },5000);
        });

        //understand button end

        $(".askbtn").click(function(){
            $('.actionBtnFloat').css('z-index','0');
            clearTimeout(interval);
            socket.emit('ask question', getUser());
            var interval = setTimeout(function(){
                $('.'+getUser()).fadeIn();
            },5000);
        });

        socket.on('ask question', function(aquestion){
            $('.cv'+aquestion).fadeOut();
            $('.'+aquestion).append('<img id="ask" class="actImage" src="images/question mark button.png" width="50" height="50">');
            $('#say'+aquestion).html('Ask a question');
            var intervals = setTimeout(function(){
                $('#say'+aquestion).html('');
                $('#ask').remove();
                $('.cv'+aquestion).fadeIn();
            },5000);
        });
        //ask question button

        //raise a hand
        $('.raisebtn').click(function(){
            $('.actionBtnFloat').css('z-index','0');
            clearTimeout(interval);
            socket.emit('raise hand', getUser());
            var interval = setTimeout(function(){
                $('.'+getUser()).fadeIn();
            },5000);
        });

        socket.on('raise hand', function(raisehands){
            $('.cv'+raisehands).fadeOut();
            $('.'+raisehands).append('<img id="rhand" class="actImage" src="images/hand button.png" width="50" height="50">');
            $('#say'+raisehands).html('Raise my hand');
            var intervals = setTimeout(function(){
                $('#say'+raisehands).html('');
                $('#rhand').remove();
                $('.cv'+raisehands).fadeIn();
            },5000);
        });
        //end raise hand

        //say i

        $('.saybtn').click(function(){
            $('.actionBtnFloat').css('z-index','0');
            clearTimeout(interval);
            socket.emit('say i', getUser());
            var interval = setTimeout(function(){
                $('.'+getUser()).fadeIn();
            },5000);
        });

        socket.on('say i', function(say){
            $('.cv'+say).fadeOut();
            $('.'+say).append('<img id="sayHi" class="actImage" src="images/me button.png" width="50" height="50">');
            $('#say'+say).html('Say I');
            var intervals = setTimeout(function(){
                $('#say'+say).html('');
                $('#sayHi').remove();
                $('.cv'+say).fadeIn();
            },5000);
        });
            //end say i

    });

    $(function () {
        var socket = io();
        $('#m').keyup(function(e){
            if(e.keyCode==13){
                if($(this).val().length !=0){
                    sendMessage(null,null);
                }else{
                    // Empty Field
                }
            }

        });

        $('#btn-send').click(function(){
            var userInfo =  document.getElementById('iammei').textContent;
            if($('#m').val().length !=0){
                socket.emit('chat live', "<b>"+userInfo+"</b>",$('#m').val());
                $('#m').val('');
                return false;
            }
        });
        socket.on('chat live', function(person,chatmes){
            $('#messages').append(person+": "+chatmes+"<br/>");
            // window.scrollTo(0, document.body.scrollHeight);
            var elem = document.getElementById('receiveMessageArea');
            elem.scrollTop = elem.scrollHeight;
        });
    });

    $('.tab-content').slimScroll({
      color: '#00f'
    });

    $('.conPanel').slimScroll({
      color: '#00f',
      height: '383px'
    }); 

    $(document).ready(function(){
      $("#actiontoggle").click(function(){
        $(".actionPanel").toggle("fast");
        $("#actiontoggle2").toggle();
      });
      $("#actiontoggle2").click(function(){
        $(".actionPanel2").toggle("fast");
        $("#actiontoggle").toggle();
      });
    });

    </script>
    {{#if teach_val}}
    <script type="text/javascript">
    var socket = io();
    $(document).ready(function(){
      setInterval(function(){
           refreshRoomList();
        },2000);
    });

    socket.on('tech notif', function(stud,stat){
        if (stat=='leave') {
            $('#studalert').val(stud+' is '+stat+' to your room');
        }else if (stat=='enter') {
            $('#studalert').val(stud+' is '+stat+' to your room');
        }

        $("#studalert").trigger( "click" );
    });
    </script>
    {{/if}}

    <script type="text/javascript">
    // Entering fullscreen mode
    $('#videoSrcBlk').click(function() {
        $("#full-screen").click(function() {
            $(this).attr('id');
            var vid = document.getElementById('selfVideo');    
            vid.removeAttribute("controls");

            if (vid.requestFullscreen) {
                vid.requestFullscreen();
            } else if (vid.mozRequestFullScreen) {
                vid.mozRequestFullScreen(); // Firefox
            } else if (vid.webkitRequestFullscreen) {
                vid.webkitRequestFullscreen(); // Chrome and Safari
            }
        });
    });

    $('.stopscreen').click(function() {
        if( $('#userfloat').is(':visible') ) {
            $('#userfloat').fadeOut();
        }
        else {
            $('#userfloat').fadeIn();
        }
    });

    $('.chatbox').click(function() {
        $('.actionBtnFloat').css('z-index','0');
        $(".commentFloat").slideToggle( "slow", function() {
            // Animation complete.
        });
    });

    $('.actionBtnFloat').click(function() {
        $(this).css('z-index','1001');
    });

    $('#mask').click(function() {
        $('.actionBtnFloat').css('z-index','0');
    });
    </script>
    <!-- Jquery Core Js -->
    <script src="plugins/jquery/jquery.min.js"></script>

    <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <!-- Bootstrap Core Js -->
    <script src="plugins/bootstrap/js/bootstrap.js"></script>

    <!-- Select Plugin Js -->
    <script src="plugins/bootstrap-select/js/bootstrap-select.js"></script>

    <!-- Slimscroll Plugin Js -->
    <script src="plugins/jquery-slimscroll/jquery.slimscroll.js"></script>

    <!-- Bootstrap Notify Plugin Js -->
    <script src="plugins/bootstrap-notify/bootstrap-notify.js"></script>

    <!-- Waves Effect Plugin Js -->
    <script src="plugins/node-waves/waves.js"></script>

    <!-- Jquery CountTo Plugin Js -->
    <script src="plugins/jquery-countto/jquery.countTo.js"></script>

    <!-- Morris Plugin Js -->
    <script src="plugins/raphael/raphael.min.js"></script>
    <script src="plugins/morrisjs/morris.js"></script>

    <!-- ChartJs -->
    <script src="plugins/chartjs/Chart.bundle.js"></script>

    <!-- Sparkline Chart Plugin Js -->
    <script src="plugins/jquery-sparkline/jquery.sparkline.js"></script>

    <!-- Custom Js -->
    <script src="js/admin.js"></script>
    <script src="js/pages/ui/notifications.js"></script>

    <!-- Demo Js -->
    <script src="js/demo.js"></script>
    {{else}}
    <!-- Jquery Core Js -->
    <script src="/plugins/jquery/jquery.min.js"></script>

    <!-- Bootstrap Core Js -->
    <script src="/plugins/bootstrap/js/bootstrap.js"></script>

    <!-- Waves Effect Plugin Js -->
    <script src="/plugins/node-waves/waves.js"></script>

    <!-- Validation Plugin Js -->
    <script src="/plugins/jquery-validation/jquery.validate.js"></script>

    <!-- Custom Js -->
    <script src="/js/admin.js"></script>
    <script src="/js/pages/examples/sign-in.js"></script>
    {{/if}}
    </body>
    </html>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.