javascript - sqlite3 callbacks (node.js) - Stack Overflow

I'm trying to pare an entered email on my website, to ones in the database to see whether it alrea

I'm trying to pare an entered email on my website, to ones in the database to see whether it already exists. If it does, then the function returns false and an error is displayed.

var db = new sqlite3.Database('users_db.db');

db.get(
    "SELECT * FROM users WHERE useremail = ?", 
    [email], 
    function (err, rows) {
        if (rows == undefined ){
            global.returnvalue2 = false;
        }
    }
);

What I want is for the function to be run immediately after the selection, so that the returned value is false, and the user record is not created.

At the moment I realise that the callback is being called after everything, so its just making the selection and carrying on throughout the rest of the program until the end.

How can I check if there are any existing records with the same email?

I'm trying to pare an entered email on my website, to ones in the database to see whether it already exists. If it does, then the function returns false and an error is displayed.

var db = new sqlite3.Database('users_db.db');

db.get(
    "SELECT * FROM users WHERE useremail = ?", 
    [email], 
    function (err, rows) {
        if (rows == undefined ){
            global.returnvalue2 = false;
        }
    }
);

What I want is for the function to be run immediately after the selection, so that the returned value is false, and the user record is not created.

At the moment I realise that the callback is being called after everything, so its just making the selection and carrying on throughout the rest of the program until the end.

How can I check if there are any existing records with the same email?

Share Improve this question edited Sep 13, 2015 at 16:00 GiamPy 3,5703 gold badges31 silver badges52 bronze badges asked Sep 13, 2015 at 15:51 Constantly ConfusedConstantly Confused 6354 gold badges11 silver badges25 bronze badges 3
  • Why can you not create the user record inside the callback? – Soren Commented Sep 13, 2015 at 15:58
  • @Soren Because the system runs through 3 checks for the email, password and the "class". If all 3 return true, then the record is created. Because node is skipping this last check of the email, it is returning 'true', and therefore the record is created before the callback is run. – Constantly Confused Commented Sep 13, 2015 at 16:02
  • So make a cascading callback -- you cannot do this with sequential programming – Soren Commented Sep 13, 2015 at 16:37
Add a ment  | 

2 Answers 2

Reset to default 4

Make use of the async features in javascript, so your code would look something like this;

var db = new sqlite3.Database('users_db.db');
function checkemail(email, cb) {
    db.get(
         "SELECT * FROM users WHERE useremail = ?", 
         [email],  
         function (err, rows) {
            if (err || rows == undefined ){
                cb("bad email", null)
            } else {
                cb(null,rows)
            } 
         });
}
function checkpassword(pw,cb) {....}
function checkclass(cls,cb) {....}

and then write you code like this;

checkemail(myemail, function(err,rows) {
    if (err) return alert(err);
    checkpassword(pw, function(err, msg) {
       if (err) return alert(err);
       checkclass(cls, function(err, msg) {
           if (err) return alert(err);
           alert("Congratulation you passed all the checks");
       });
    });
});

Here's a little one I made.

const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('iHacks.db');

function get_user_credentials (email, password)
{ return new Promise((rs, rj) => {
  function callback (err, User)
  {
    if (err) rj(err);
    rs(User);
  }

  db.get('select * from users where email=? and password=?', [email, password], callback);
}); }

function login (email, password)
{ return new Promise ((rs, rj) => {
  // Hashing the password.
  password = sha256(password + 'EzSalt');

  // Creating an Error
  const err = new Error('Email or password did not match!');

  // Callback functions
  function check (User)
  {
    rs(User);
  }      

  function fail (err)
  {
    rj(err);
  }

  // Getting the user credentials
  get_user_details(email, password).then(check).catch(fail);    

}); }

login()
  .then(/* Continue code */)
  .catch(err => {throw new Error(err); })
  ;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744704408a4588966.html

相关推荐

  • javascript - sqlite3 callbacks (node.js) - Stack Overflow

    I'm trying to pare an entered email on my website, to ones in the database to see whether it alrea

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信