简体   繁体   中英

Node.js Sqlite3 if row exists

Is there any way to check if a row exists using node.js and the sqlite module. This function is the best i have now, but it always returns false between the module is asynchronous

function auth(uname, pwd) {
    var userExists = false;
    authDB.serialize(function() {
        authDB.each("SELECT * FROM Users WHERE username = '" + uname + "' AND password = '" + pwd + "' LIMIT 1)", function(err, row) {
            userExists = true;
            console.log(userExists)
        });
    });
    return userExists;
}

Is there any way to do this?

Thanks

Check the value of row , that is the result of your query. Depending on your database driver you will get [] or null in case the row did not exist.

I never used sqlite3 in node, but you can easily check what is the value, by simply searching for a row that for sure does not exist.

And BTW you should return the userExists from inside the callback:

 function(err, row) {
        if (row != ...
             return true;
        userExists = true;
        console.log(userExists)
    });

because the database query is asynchronous. But in your code you're returning userExists from outside the callback that will always occur earlier than your database query finishes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM