0

i have a register form and i need to send the error message when username exist or email exist:

nodejs code :

app.post('/register', function(req, res) {


  var entity = _.pick(req.body, 'password', 'fullName', 'email', 'userName', 'phone', 'companyName', 'companyWebsite');
  // hash password
  entity.password = bcrypt.hashSync(entity.password, config.SALT_WORK_FACTOR);
  async.waterfall([
    function(cb) {
      if(entity.userName) {

        userService.findByUsername(entity.userName, function(err, user) {
          if(err) {

            cb(err);


          } else if(user) {



            cb(new errors.ValidationError('Cannot create user, userName already exists', httpStatus.BAD_REQUEST));


          } else {


            cb();
          }
        });
      } else {

        cb();
      }
    },
    function(cb) {

      if(entity.email) {

        userService.findByEmail(entity.email, function(err, user) {
          if(err) {

            cb(err);
          } else if(user) {

            cb(new errors.ValidationError('Cannot create user, email already exists', httpStatus.BAD_REQUEST));

          } else {
            cb();
          }
        });
      } else {
        cb();
      }
    },
    function(cb) {

      crud.create(entity, cb);

    },

 ], function(err, createdEntity) {
   data = {
     statusCode: httpStatus.CREATED,
      content: createdEntity
   };

if(err)
{


res.render('register', {user : undefined, success :true,successs :true});

}

else{
res.render('login', {user : undefined, success :true});
}


 });


}); 

me ejs file code is :

     <div class="alert">
  <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span> 
  <strong>oops!</strong>Cannot create user, username already exists
</div>


           <div class="alert">
  <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span> 
  <strong>oops!</strong>Cannot create user, email already exists
</div>

how can i pass or send the message error to ejs file ..the message error show in nodejs code under if(user):cb(new errors.ValidationError('Cannot create user, userName already exists', httpStatus.BAD_REQUEST));

1 Answer 1

3

You can Print the data passed from server side part to ejs using

<%=%>

for example: <%=user%> will print user.

When err object exsists send it as third object to the ejs file and read it in the same way.

if(err){
res.render('register', {user : undefined, success :true,successs :true,error: err});
}

and in the View

<%if(typeof error == "undefined"){%>
        //Show some HTML here
 <% } else { %>
         //Show other html when you send error obejct sent
         <%= error.message%> //For Example
    <%}%>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.