I am working on creating a user registration system for a website that I am working on, but I have a few problems.
I try to stay away from having to insert callbacks because it gets dirty. I need help if you find out if there is a way to create synchronous queries using node -mysql
This is what I am trying to achieve.
connection.query("select 1 as email from users where email = " + connection.escape(email), function(err, rows, fields) { if(err) { var error = { error_message: err.code, error_number: err.errno }; return res.send(error); } if(rows.length > 0) { var error = { message: 'Email Address is Taken', code: 2 }; return res.send(error); } }); connection.query("insert into users (email, password) values ("+connection.escape(email)+", "+connection.escape(hash)+")", function(err, rows, fields) { if(err) { var error = { error_message: err.code, error_number: err.errno }; return res.send(error); } });
My goal is to execute the first query, and if it returns a string and then does not execute the second query, but if the first query returns 0 rows, continue and run the second query.
I know that I can embed the second request in the first request and put, if in another, but what I do not want to do, because when I have these two requests, I also have the option to use bcrypt to encrypt the password, which should also be nested.
Is there a way to write it down so that I don’t need to insert two queries or nesting them will be my only option?
source share