Search code examples
javascriptnode.jsdatabaseobjectnode-sqlite3

Sqlite3 (node.js) returns [object Object]


I am trying to use SQLITE3 to store data for my discord bot and here i need to save a number that goes up each time a command is used. Here is my code: (creating table and setting the value to 0.)

  db.run("CREATE TABLE gbanCases ( number INT )")
  db.run("INSERT INTO gbanCases (number) VALUES ($number)", {
    $number : 0
});

Trying to see what number returns:

        var caseNumber = db.run("SELECT number FROM gbanCases")
        console.log(caseNumber)

The thing is that caseNumber returns me "[object Object]" and I want it to return the 0 that was setted up earlier. Please help me if you can, i'm still very new to this.


Solution

  • Use db.get() to get the result of a query, not db.run(). The resulting row is passed as an argument to the callback function.

    db.get("SELECT number FROM gbanCases", function(err, row) {
        if (err) {
            throw err;
        }
        console.log(row.number);
    })