0

I have a need, for example, to return information about whether the user is blocked, obtained from the database, to the main code. I created a class called Db:

export class Db {
    constructor() {
        this.database = database;
    }

    private async databaseGet(sql: string, params: any[], callback: Function) {
        database.all(sql, params, async (err: Error | null, row: any) => {
            await callback(row)
        })
    }


    async checkUserban(userId: number): Promise<boolean | any> {
        let res

        async function resultFunc(rows: object) {
            console.log(rows)
            res = rows
        }

        await this.databaseGet('SELECT id FROM bans WHERE user_id = ?', [userId], resultFunc)

        console.log(res)
    }
} 

Next, I need to analyze the rows from the database, and if there is a user with this id in the table, return tyrue, if not, return false. The problem is that I will receive the response from the database later than the code in the class function is executed, therefore, it will not be possible to receive the response from the database to process it. How to implement this?

Nothing came to mind

2
  • What is database here? Commented Apr 25, 2024 at 12:48
  • import {Database, Statement} from "sqlite3" const database = new Database('database.sqlite3') Commented Apr 25, 2024 at 12:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.