使用hbs engin和res.render在页面内显示页面

时间:2019-03-17 20:32:08

标签: node.js express express-handlebars

所以我有这个登录页面,要求用户输入2次输入,当用户输入错误的值时,它应该显示错误消息(文本行),而是在当前页面内显示整个页面,并显示该消息在此页面中。 enter image description here

这是我的hbs代码:

<form  action="/loginadmin" method="POST">
        <div class="login100-form validate-form p-l-55 p-r-55 p-t-178" >
					<span class="login100-form-title">
						LogIn تسجيل الدخول
					</span>

					<div class="wrap-input100 validate-input m-b-16" data-validate="Please enter username">
						<input type="text" name="Mobilenumber" id="Mobilenumber" placeholder="Mobile Number رقم الجوال">
						<span class="focus-input100"></span>
					</div>

					<div class="wrap-input100 validate-input" data-validate = "Please enter password">
						<input  type="password" name="pass" id="pass" placeholder="Password الرقم السري">
						<span class="focus-input100"></span>
					</div>
</br>
					<div class="container-login100-form-btn">
						<button type="submit">
						Login دخول
						</button>
					</div>
             <p>{{ErrorMessage}}</p> <!-- this supposed to show error message -->

这是index.js部分:

//to access the page:
router.get('/Loginpage', function(req, res, next){
  res.render('/loginpage/index', {ErrorMessage: ''});

});

//form submission:
router.post('/loginadmin', function(req, res, next) {
  //take from input:

var mobileValue = req.body.Mobilenumber;
var passwordValue = req.body.pass;
console.log(mobileValue);
console.log(passwordValue);
// database connection to mongo .. removed for simplicity
  collection.find({mobile: mobileValue}).toArray(function(err, docs) {
    if(!docs.length ){
      console.log('mobile is not registered');
      res.render("loginpage/index", {ErrorMessage: 'mobile is not registered'});//this part is causing the trouble I don't know why
    }else{
    //somethingelse//}
});

谢谢

0 个答案:

没有答案