How can I check if an email already is in Mysql database on NodeJs? I'm using the following code
app.use(express.static(__dirname '/public'));
router.get('/',function(req,res) {
res.sendFile(path.join(__dirname '/public/html/index.html'));
var email = req.query.email;
if (email != null) {
conn.query('INSERT INTO users (email) VALUES (?)', [email], function(err, result) {
if (err) throw err;
console.log('1 record inserted')
})
}
});
CodePudding user response:
This is an example from a store stay project I had. I use this method because it is convenient for me.
const [isItemExist] = await SQL(`
SELECT * FROM products WHERE products.id =${Item} `)
if (!isItemExist) {
return res.send({ err: 'product not found' })
}
CodePudding user response:
Check if e-mail exist only do insert if it doesn't:
router.get('/', function(req, res){
res.sendFile(path.join(__dirname '/public/html/index.html'));
var email = req.query.email;
if (email != null) {
conn.query('SELECT * FROM users WHERE email=?', [email], function(err, result) {
if (err) throw err;
if (result.length > 0) {
console.log('Email already exist')
}
else {
conn.query('INSERT INTO users (email) VALUES (?)', [email], function(err, result) {
if (err) throw err;
console.log('1 record inserted')
})}
})
}
})