Node.js Login Crashes App because Mysql "results" are "undefined" - mysql

I am extremely close to this working but for some reason the browser is rendering the standardized var of "results" as undefined.
` var email = request.body.email;
var password = request.body.password;
if (name && password) {
userDB.query('SELECT * FROM accounts WHERE name = ? AND password = ?', [email, password], function (err, results, fields) {
if (results.length > 0) { // <-- This is it
request.session.loggedIn = true;
request.session.email = email;
//loggedIn = true;
response.redirect('/');
} else {
response.render('test');
loggedIn = false;
}
response.end();
});
}`
Here is the actual err in the console:
TypeError: Cannot read property 'length' of undefined
I thought "results" and "fields" were standard. What am I missing?
If need be I can always reply w/ more code to clarify.

Related

Update only one column in user info - MySQL NodeJS

I'm builing a classic CRUD (create, read, update, delete) API with NodeJS/Express and MySQL.
I created a route to update my user informations that works fine.
The problem :
If I dont send EVERY data (first_name, last_name etc...), the columns with no data update to undefined in MySQL database, and the column with data don't update. I would like that if I don't send data, no change happens for the columns, and only the one with datas change.
Here is my controller :
module.exports.updateUser = (req, res, next) => {
if (req.method == "PUT") {
let userDetails = `UPDATE users SET first_name = '${req.body.first_name}', last_name = '${req.body.last_name}', user_name = '${req.body.user_name}' WHERE id = ${req.params.id}`;
sql.query(userDetails, function (err, result) {
if (!err) {
res.status(200).json({ message: "User infos updated." })
} else {
res.status(401).json({ message: "Error when updating user infos." })
}
})
}
}
So, if I make a PUT request on an existing user in db with only the mail for example :
{
"mail": "test2#test2.com"
}
all my user datas become null and user.mail stays the same.
Anyone could help me on this ?
Thank you 🙂
Use this query for update one or more filled update at a time
module.exports.updateUser = (req, res, next) => {
if (req.method == "PUT") {
let query = '';
if(req.body.first_name){
query = `first_name =
'${req.body.first_name}'`
}else
if(req.body.last_name){
query = ` last_name =
'${req.body.last_name}'`
}else
if(req.body.user_name){
query = `user_name =
'${req.body.user_name}'`
}eles if(req.body.first_name
&& req.body.last_name)
{
query =`first_name = '${req.body.first_name}', last_name = '${req.body.last_name}'`
}else if(
req.body.last_name && req.body.user_name
{
query = `last_name = '${req.body.last_name}', user_name = '${req.body.user_name}'`
}else if(req.body.first_name
&& req.body.last_name && req.body.user_name)
{
query =`first_name = '${req.body.first_name}', last_name = '${req.body.last_name, user_name = '${req.body.user_name}'}'`
}
let userDetails = `UPDATE users SET ${query} WHERE id = ${req.params.id}`;
sql.query(userDetails, function (err, result) {
if (!err) {
res.status(200).json({ message: "User infos updated." })
} else {
res.status(401).json({ message: "Error when updating user infos." })
}
})
}
}
Thanks to #jutendra, it works.
After few months (and gained skills), here is a cleaner version if ever someone is interested :
module.exports.updateUser = (req, res, next) => {
if (req.method !== "PUT") return;
const updates = [];
if (req.body.first_name) updates.push(`first_name = '${req.body.first_name}'`);
if (req.body.last_name) updates.push(`last_name = '${req.body.last_name}'`);
if (req.body.user_name) updates.push(`user_name = '${req.body.user_name}'`);
if (updates.length === 0) {
res.status(400).json({ message: "No updates provided" });
return;
}
const query = `UPDATE users SET ${updates.join(", ")} WHERE id = ${req.params.id}`;
sql.query(query, (err, result) => {
if (!err) {
res.status(200).json({ message: "User infos updated." });
} else {
res.status(401).json({ message: "Error when updating user infos." });
}
});
};

MYSQL length of results undefined

*throw err; // Rethrow non-MySQL errors
^
TypeError: Cannot read property 'length' of undefined*
db.js:
function validateUser(username, password) {
var sql = "SELECT * FROM users WHERE username = ? AND password = ?";
var values = [[username, password]];
con.query(sql,[values], function(err, results) {
if (results.length > 0){
return true;
}
else return false;
})
}
server.js:
app.post('/auth', function(req,res) {
console.log('form submitted');
if(db.validateUser(req.body.username,req.body.password)){
console.log('login successful');
}
res.status(200);
res.redirect(URL);
});
First please don't save passswords as plain text.
for example https://cdnjs.com/libraries/jsSHA
For your code:
function validateUser(username, password) {
var sql = "SELECT * FROM users WHERE username = ? AND password = ?";
var values = [username, password];
con.query(sql,values, function(err, results) {
if (results.length > 0){
return true;
}
else return false;
})
}

if -else condition is not working in node js

What I am trying to implement is that after logged in ,system will check user's role and redirect the user accordingly.
1- Admin
2- User
role field is integer type. below is my code
router.post('/signin', function (req, res, next) {
session_store = req.session;
req.assert('Emailid', 'Please fill register Email Address').notEmpty();
req.assert('Emailid', 'Email not valid').isEmail();
req.assert('password', 'Please fill the Password').notEmpty();
var errors = req.validationErrors();
if (!errors) {
Emailid = req.sanitize('Emailid').escape().trim();
password = req.sanitize('password').escape().trim();
var query = 'select * from userdetails where Emailid=? and password=?';
var sql='select role from userdetails where Emailid=?'
db.query(query, [Emailid, password], function (err, rows) {
if (err) {
var errornya = ("Error Selecting : %s ", err.code);
console.log(err.code);
req.flash('msg_error', errornya);
res.redirect('/login-Form');
} else {
if (rows.length <= 0) {
req.flash('msg_error', "Wrong email address or password. Try again.");
res.redirect('/login-Form');
}
else {
session_store.is_login = true;
session_store.user = Emailid;
db.query(sql, Emailid, function (err, result) {
if (err) throw err
else {
if (result == 1) { // facing issue here. It is directly going to else block though the user role is 1 in the mysql table
console.log(result)
res.redirect('/Dashboard');
}
else {
res.redirect('/Audit-Record');
}
}
});
}
}
});
}
else {
res.redirect('/login-Form');
}
});
I guess I am making some mistake while comparing the result value. Can anyone of you please check and let me know where I am going wrong.
Thanks in advance!
The issue got resolved. Actually I was comparing in wrong way. we need to write it like this
else {
session_store.is_login = true;
session_store.user = Emailid;
db.query(sql, Emailid, function (err, result) {
if (err) throw err
else {
**if (result[0].role == 1)** {
console.log(result)
res.redirect('/Dashboard');
}
else {
res.redirect('/Audit-Record');
}
}
});

Node.js sequential mysql queries promise not resolving

A route in app.js calls a function register(user) in a MySQL model, model.js. This register() calls displayNameTaken(display_name) which will return null if display name is available otherwise it will return a json object.
The promise in the app.post containing model.register(req.body) does not resolve.
If display name is taken register() will pass this json object back to the calling route.
If display name is not taken register() will register user and return back another json object back to the calling route.
The app never resolves the returned promise, app#113.
Or do you have any suggestions to what I should do instead?
Can you see what I have done wrong?
Output below:
1. When display name taken
app#113 [ undefined ]
model#73 { code: 12, message: 'e' }
2. Display name not taken, registration successful
app#113 [ undefined ]
model#73 undefined
model#61 110 //<- last insert id
The app never resolves the returned promise, app#113.
Or do you have any suggestions to what I should do instead?
Can you see what I have done wrong?
app.post('/api/register', function (req, res) {
Promise.all([
model.register(req.body)
]).then((r => {
console.log('app#113',r);// r=> undefined
res.status(200).json(r);
})).catch((e => {console.log(e);
res.status(500).json(e);
}));
});
function Model(db){
this.db = db;
}
//Function returns null if display name is not taken
Model.prototype.displayNameTaken = function(display_name){
return new Promise((resolve, reject, next) => {
var sql = "SELECT id FROM `users` WHERE `display_name` = ?";
var rv;
this.db.query(sql, [[display_name]], (err, result) => {
if (err) {
return resolve(err);
}
if(0 < result.length && result[0].id != undefined && result[0].id != NaN && 0 < result[0].id){
rv = {code: 12, message:'e'};
}else{
rv = null;
}
return resolve(rv);
});
});//Promise
}
model.register = function register(params){
if(params == undefined){
return;
}
var rv;
Promise.all([
this.displayNameTaken(params.display_name.trim())
]).then((r => {
return new Promise((resolve, reject, next) => {
if(r[0] == null){//display_name available
var sql = "INSERT INTO `users` (`display_name`, `email`, `hash`, `created`,`md51`, `md52`, `language`) VALUES ?";
var md51 = md5(randomString({length:32}));
var md52 = md5(randomString({length:32}));
var user = [[
params.display_name.trim(),
params.email.trim(),
passwordHash.generate(params.hash.trim()),
datetime.create().format('Y-m-d H:M:S'),
md51,
md52,
params.language
]];
this.db.query(sql, [user], function (err, result) {
if (err) {
return reject(err);
}
console.log('model#61',result.insertId);
if(0 < result.insertId){
rv = {code: 8, message:'i', md51: md51, md52: md52};
}else{
rv = {code: 0, message:'e'};
}
return resolve(rv);
});
}else{//display_name taken
rv = r[0];
}
console.log('model#73',rv);
return resolve(rv);
});//Promise
})).catch((e => {
console.log(e);
}));

How to add session and add access token to login in node

I have a login api as follows :
app.post('/login', function (req, res){
email = req.body.email;
password = req.body.password;
if(email && password )
{
console.log(email); //displays the email in the terminal
console.log(password); //displays the password in the terminal
var status = []; //array to output json
//connection.connect(); //mysql connection
connection.query('SELECT username FROM user WHERE email =? and password = ?',[email,password], function (error, rows, fields) {
if(rows.length == 1)
{
passport.serializeUser(function(res, done) {
done(null,res);
});
passport.deserializeUser(function(id, done) {
done(null,res);
});
status.push({username: rows[0].username});
username = rows[0].username;
console.log('sucessful login');
res.end('"status:"\n'+JSON.stringify(status)); //output as JSON
}
if(rows.length == 0)
{
connection.query('select * from temp_users where email = ? and password = ?',[email,password],function(error,rows,fields)
{
status = '';
if(rows.length == 1)
{
status = 'pending';
//res.end('Your Account needs to be verified');
res.end('"status:"'+JSON.stringify(status));
console.log('Your Account needs to be verified.');
}
else
{
status = 'error';
res.end('"status:"'+JSON.stringify(status));
//res.end('no such user.Please create an account');
console.log('no such user.Please create an account');
}
}
);
}
});
}
});
Now what i would like to do is to add a session for every user that logs in and make that session expire after some time.I would also like to have an access token to be inserted to my database once the user logs in.
Any help?
This example does exactly what you are looking for.
node.js express mysql passport