X press publishing sql error

Hi everyone, I am having a sql error, and I am at step 44 in the project where I suppose to add a series and save it, but when I press to save it I get the sql error of not having the series table. I don’t know what to do, I tried to find a solution but no success. Can you help me?

Sql error

Migration.js

series.js

const seriesRouter = express.Router();

const sqlite3 = require('sqlite3');
const db = new sqlite3.Database(process.env.TEST_DATABASE || './database.sqlite');

const issuesRouter = require('./issues.js');

seriesRouter.param('seriesId', (req, res, next, seriesId) => {
  const sql = 'SELECT * FROM Series WHERE Series.id = $seriesId';
  const values = {$seriesId: seriesId};
  db.get(sql, values, (error, series) => {
    if (error) {
      next(error);
    } else if (series) {
      req.series = series;
      next();
    } else {
      res.sendStatus(404);
    }
  });
});

seriesRouter.use('/:seriesId/issues', issuesRouter);

seriesRouter.get('/', (req, res, next) => {
  db.all('SELECT * FROM Series', (err, series) => {
    if (err) {
      next(err);
    } else {
      res.status(200).json({series: series});
    }
  });
});

seriesRouter.get('/:seriesId', (req, res, next) => {
  res.status(200).json({series: req.series});
});

seriesRouter.post('/', (req, res, next) => {
  const name = req.body.series.name,
        description = req.body.series.description;
  if (!name || !description) {
    return res.sendStatus(400);
  }

  const sql = 'INSERT INTO Series (name, description) VALUES ($name, $description)';
  const values = {
    $name: name,
    $description: description
  };

  db.run(sql, values, function(error) {
    if (error) {
      next(error);
    } else {
      db.get(`SELECT * FROM Series WHERE Series.id = ${this.lastID}`,
        (error, series) => {
          res.status(201).json({series: series});
        });
    }
  });
});

seriesRouter.put('/:seriesId', (req, res, next) => {
  const name = req.body.series.name,
        description = req.body.series.description;
  if (!name || !description) {
    return res.sendStatus(400);
  }

  const sql = 'UPDATE Series SET name = $name, description = $description ' +
      'WHERE Series.id = $seriesId';
  const values = {
    $name: name,
    $description: description,
    $seriesId: req.params.seriesId
  };

  db.run(sql, values, (error) => {
    if (error) {
      next(error);
    } else {
      db.get(`SELECT * FROM Series WHERE Series.id = ${req.params.seriesId}`,
        (error, series) => {
          res.status(200).json({series: series});
        });
    }
  });
});

seriesRouter.delete('/:seriesId', (req, res, next) => {
  const issueSql = 'SELECT * FROM Issue WHERE Issue.series_id = $seriesId';
  const issueValues = {$seriesId: req.params.seriesId};
  db.get(issueSql, issueValues, (error, issue) => {
    if (error) {
      next(error);
    } else if (issue) {
      res.sendStatus(400);
    } else {
      const deleteSql = 'DELETE FROM Series WHERE Series.id = $seriesId';
      const deleteValues = {$seriesId: req.params.seriesId};

      db.run(deleteSql, deleteValues, (error) => {
        if (error) {
          next(error);
        } else {
          res.sendStatus(204);
        }
      });
    }
  });
});

module.exports = seriesRouter;
type or paste code here

Hello :slight_smile:

It seems that you have too many databases.

In migration.js you define link to the database in this line:

const db = new sqlite3.Database('./database.sqlite');

that’s completely fine, ./database.sqlite means "get file database.sqlite which is placed in the same directory as the script migration.js. And we have that file right here:

Now let’s check what happens in the api/series.js script. Now you are referencing the database that has the same name (assuming that process.env.TEST_DATABASE is empty):

const db = new sqlite3.Database(process.env.TEST_DATABASE || './database.sqlite');

but it’s not the same database. In this script you are referencing database named database.sqlite that is placed in the same directory as the file series.js - api directory, this one:

So you have created all the tables in the database database.sqlite but you are trying to query database api/database.sqlite, that’s why you are getting an error.


To fix this problem you should delete file api/database.sqlite and use path ../database.sqlite to reference your database when you are inside api directory (for example in api/series.js script). .. means “parent directory”.

Thank you for answering me, I deleted the api/database and I updated the path to database for artists.js, series. js and issues. js but still I get the same sql error of no having the tables. :unamused: