How to use async/await in sqlite3 db.get and db.all?

Here is my server code, which is as follows,

const express = require("express");
const app = express();
const fs = require("fs");

const dbFile = "./sqlite.db";
const exists = fs.existsSync(dbFile);
const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database(dbFile);

app.get("/", async (req, resp) => {
  await db.run(`INSERT INTO Times VALUES (${ Date.now() })`);
  let rows = await db.all("SELECT time FROM Times");
  console.log(rows); // The line where I console log rows
  resp.send(rows);
});

app.listen(process.env.PORT || 8080);
process.on("uncaughtException", console.log);

The above server is logging a database object like so,

Database {}

in console every time I refresh the site, but I expect it to log the row which I’ve inserted in the database.

What I’m doing wrong here?

@vrintle, what is the current output that is getting logged? Perhaps a screenshot would help.

@khalby786 It’s logging Database {} only. You can check it now.

I mistakenly deleted the db file, so you might not be getting the error.

1 Like

I am stuck in the same error for the last 3 hours and when I finally found someone with the same problem, nobody answered him :frowning:

Hi, what problem are you exactly facing?

try this

await new Promise((resolve,reject)=>{
db.all("SELECT time FROM Times",[],(err,rows)=>{
         if(err)reject(err)
          resolve(rows)
})
})

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.