user2330561
user2330561

Reputation: 495

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

Upvotes: 0

Views: 4982

Answers (1)

Pio
Pio

Reputation: 4064

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.

Upvotes: 1

Related Questions