Reputation: 191
So I have this query and I am bit confused about how the logic works. I am creating a new user registration method, and inside it there's a checking method whether the user is using the same email or not. I am using POSTMAN when posting the query.
The first query works fine, both the email checking method and registering the new one, while for the second one, the email checking works but registering with new email doesn't work, POSTMAN keeps loading until a minute or so until it says 'Couldn't get a response'.
First One (Works)
exports.postContact = router.post("/api/users/new", (req, res) => {
var today = new Date();
var users = {
"first_name": req.body.first_name,
"last_name": req.body.last_name,
"email": req.body.email,
"password": req.body.password,
"created": today,
"modified": today
}
var email = req.body.email;
mySQL.query('SELECT * FROM users WHERE email = ?', [email], function (err, results, fields) {
if (results.length > 0) {
if (results[0].email == email) {
res.send({
"code": 204,
"status": 'Failed',
"message": "This email address is already registered, please login instead"
});
}
}
else {
mySQL.query("INSERT INTO users SET ?", users,
(err, results, fields) => {
if (err)
res.send({
"code": 400,
"status": 'Failed',
"message": 'Something wrong with the connection'
})
res.send({
"code": 400,
"status": 'Success',
"message": 'User is successfully registered'
})
}
);
}
});
});
Second One (Doesnt Work)
exports.postContact = router.post("/api/users/new", (req, res) => {
var today = new Date();
var users = {
"first_name": req.body.first_name,
"last_name": req.body.last_name,
"email": req.body.email,
"password": req.body.password,
"created": today,
"modified": today
}
var email = req.body.email;
mySQL.query('SELECT * FROM users WHERE email = ?', [email], function (err, results, fields) {
if (results.length > 0) {
if (results[0].email == email) {
res.send({
"code": 204,
"status": 'Failed',
"message": "This email address is already registered, please login instead"
});
}
else {
mySQL.query("INSERT INTO users SET ?", users,
(err, results, fields) => {
if (err)
res.send({
"code": 400,
"status": 'Failed',
"message": 'Something wrong with the connection'
})
res.send({
"code": 400,
"status": 'Success',
"message": 'User is successfully registered'
})
}
);
}
}
});
});
Upvotes: 1
Views: 76
Reputation: 11
Might want to refresh on your SQL statements.
I believe this is correct:
INSERT INTO users SET ?
Proper insert query for MySQL:
INSERT INTO table
(column1, column2, ... )
VALUES
(expression1, expression2, ... ),
(expression1, expression2, ... )
Upvotes: 1