1

I have written the code in Node.js to connect mariadb db using seneca micro service but i am not able to know how to handle error/exception and show the user a proper message. So please help me which types of error will come and how to handle it.

This is my code

seneca.add({
  role: 'user',
  cmd: 'Authentication_Registration'
}, function(msg, respond) {
  //Check wheather user is exist or not
  /*try{*/

  con.query('Select * from RegistrationDemo where UserName= (:user) ', {
    user: msg.Username
  }, function(err, results) {
    if (err)
      throw err
    else {
      if (results.length != 0) {
        respond(false, {
          answer: "User Allready Exists"
        });
      } else {
        //If user is not exist enter the registration details into the table
        con.query('insert into RegistrationDemo(Name,UserName,DOB,Email,Password) values ((:name),(:user),(:dob),(:email),(:password)) ', {
          name: msg.Name,
          user: msg.Username,
          dob: msg.dob,
          email: msg.email,
          password: msg.password
        }, function(err) {
          if (err)
            respond(false, {
              answer: "Data not able to insert"
            });
          else {
            //insert 
            con.query('insert into Hash.user(UserName,PassWord) values((:user),(:pass))', {
              user: msg.Username,
              pass: msg.password
            }, function(err) {
              if (err) {
                respond(false, {
                  answer: "Data can't registered"
                });
                con.query('rollback');
              } else {
                respond(true, {
                  answer: "you are registered sucessfully"
                });
              }
            })
          }
        });
      }
    }
  })

  /*} /*catch(Error)
    {
        console.error("catch erro" + Error);
    }*/
});

seneca.act({
  role: 'user',
  cmd: 'Authentication_Registration',
  Name: 'asn',
  Username: 'asn@123',
  email: 'xxx@gmail.com',
  password: 'xxx',
  dob: '1993-09-23'
}, function(err, result) {
  if (err) return console.error(err)
  console.log(result.answer);
});
Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
Akash Sourav Nayak
  • 203
  • 1
  • 8
  • 21
  • you can search for callback-hell, error first, promise. I believe this has been answered many times yet. –  May 13 '16 at 08:08
  • i dont think so.. bcz how to handel the error using try/catch or eventhandler is not specified so can u plz help me to figure this out – Akash Sourav Nayak May 13 '16 at 09:19

0 Answers0