我已经在 Node.js 中编写了代码以使用 seneca 微服务连接 mariadb db,但我不知道如何处理错误/异常并向用户显示正确的消息。所以请帮助我会出现哪些类型的错误以及如何处理它。
这是我的代码
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);
});