Home > Net >  Getting the result value out of Sqlite3 callback function
Getting the result value out of Sqlite3 callback function

Time:11-21

I'm using Sqlite3 in an Express back-end with a React front-end. So I'm trying to check if there is a user with a certain email in the database. I've got the function below, which is not finished yet, but the issue I'm having is that I cannot return the result.

Is there a way to return the result variable from the callback function or do I only have access to it inside the callback function?

When finished the function should be returning the email of the user if they exists or false if they don't exist in the database.

getUserByEmail = () => {
    database.get(getUserByEmailQuery, [], (err, result) => {
        if (err) {
            console.log(err.message);
        }
        console.log(result.emailAddress);
        return result;
    });
};

CodePudding user response:

I just solved it an hour or so ago. Not the smartest solution, but what I did was just use a different package for the database.

So I used better-sqlite3 which makes it possible to just store the result from the database query into a variable.

Here is the updated code:

exports.getUserByEmail = (email) => {
    const result = betterSqlite3DB.prepare(getUserByEmailQuery).get(email);
    return result;
};
  • Related