I am trying to throw an error in the following syntex:
if(err) throw err
What I expect is to print the log by the line of "console.log(err)" instead of app crashed.
However, this occurs an error with app crashed and says
throw err; // Rethrow non-MySQL errors
^
Error: Error: ER_DUP_ENTRY: Duplicate entry 'test4' for key 'nickname_UNIQUE'
I cannot figure out why this happens and need helps from the experties.
code :
api.post('/', (req, res) => {
const email = req.body.email
const nickname = req.body.nickname
const password = req.body.password
const thumbnail = req.file
const type = req.body.type
const hasherCallback = (err, pass, salt, hash) => {
if (err) throw err
const sql = `INSERT INTO users set ?`
const fields = { nickname, email, 'password': hash, salt, thumbnail, type }
const queryCallback = (err) => {
if (err) throw err
return res.json(messages.SUCCESS_MSG)
}
conn.query(sql, fields, queryCallback)
}
try {
return hasher({ password }, hasherCallback)
} catch (err) {
//handle errors here
console.log(err)
}
})
return api
}
//Error : Error: Error: ER_DUP_ENTRY: Duplicate entry 'test4' for key 'nickname_UNIQUE'