node.js – SQLITE_MISUSE error 21 when attempting to populate database with serialize (NodeJS + Express)

may I know what the issue is with this code? I have been searching over the internet for answers but it appears that this is a new problem with SQLite3. I referenced youtube videos as well and the video creators did not face such problems.

After pulling data from an external API, I am attempting to populate the Sqlite3 database. The table has already been created but for some reason Sqlite 3 keeps complaining of a MISUSE when I have already introduced async and wait into the code for the response to complete

const sqlite3 = require("sqlite3").verbose();
const axios = require("axios");

const db = new sqlite3.Database("./mock.db", sqlite3.OPEN_READWRITE, (err) => {
  if (err) {
    return console.error(err);
  console.log("Connection Successful");

// // For photo table creation. Comment out if not needed
// sql =
//   "CREATE TABLE photos(albumId, id, title, url, thumbnailUrl)";

const getData = async (url) => {
  try {
    const { data } = await axios.get(url);
    data.forEach((d) => {
      let photoData = {
        albumId: d.albumId,
        title: d.title,
        url: d.url,
        thumbnailUrl: d.thumbnailUrl,
      db.serialize(() => {
          "INSERT INTO photos (albumId, id, title, url, thumbnailUrl) VALUES(?,?,?,?,?)",
          (err) => {
            if (err) {
            } else {
              console.log("Row successfully added");
  } catch (error) {



The error message which I got was

Error: SQLITE_MISUSE: Database is closed
at Database.<anonymous>
at Array.forEach (<anonymous>)
    at getData 26:10)
    at processTicksAndRejections (internal/process/task_queues.js:95:5) {
  errno: 21,
  code: 'SQLITE_MISUSE',
  __augmented: true

Read more here: Source link