Following very closly instructions from X-Press Publishing project I get stuck on Step 35.
Link to the project:
https://www.codecademy.com/paths/web-development/tracks/building-a-persistent-api/modules/persistent-api-cumulative-projects/projects/x-press-publishing
The problem is that I can’t do anything with the app. For example, if I try to add a new artist, when I fill in all fields and press Save nothing happens. I can’t also see any saved artists.
One thing to highlight, I have all tests passed fo far, no errors (log files attached)
.My code:
server.js
//jshint esversion:6
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const cors = require('cors');
const errorhandler = require('errorhandler');
const PORT = process.env.PORT || 4001;
const app = express();
const apiRouter = require('./api/api.js');
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use(cors());
app.use('/api', apiRouter);
app.use(errorhandler());
app.listen(PORT, () => {
console.log(`The server is listening on port: ${PORT}`);
});
module.exports = app;
api.js
//jshint esversion:6
const express = require('express');
const apiRouter = express.Router();
const artistsRouter = require('./artists.js');
apiRouter.use('/artists', artistsRouter);
module.exports = apiRouter;
artists.js
//jshint esversion:6
const express = require('express');
const sqlite3 = require('sqlite3');
const artistsRouter = express.Router();
const db = new sqlite3.Database(process.env.TEST_DATABASE || './database.sqlite');
///////////////////////artistId middleware////////////////////////////
artistsRouter.param('artistId', (req, res, next, artistId) => {
const sql = 'SELECT * FROM Artist WHERE Artist.id = $artistId';
const values = {
$artistId: artistId
};
db.get(sql, values, (error, artist) => {
if (error) {
next(error);
} else if (artist) {
req.artist = artist;
console.log('This logs req.artist: '+ req.artist);
next();
} else {
res.sendStatus(404);
}
});
});
//////////////////////////GET////////////////////////////////////
artistsRouter.get('/', (req, res, next) => {
db.all(`SELECT * FROM Artist WHERE Artist.is_currently_employed = 1`,
(error, artists) => {
if (error) {
next(error);
} else {
res.status(200).json({artists: artists});
}
});
});
artistsRouter.get('/:artistId', (req, res, next) => {
res.status(200).json({artist: req.artist});
});
/////////////////////////////POST/////////////////////////////////
artistsRouter.post('/', (req, res, next) => {
const name = req.body.artist.name;
const dateOfBirth = req.body.artist.dateOfBirth;
const biography = req.body.artist.biography;
const isCurrentlyEmployed = req.body.artist.is_currently_employed === 0 ? 0 : 1;
if (!name || !dateOfBirth || !biography) {
return res.sendStatus(400);
}
const sql = (`INSERT INTO Artist (name, date_of_birth, biography,
is_currently_employed) VALUES ($name, $dateOfBirth, $biography,
$isCurrentlyEmployed)`);
const values = {
$name: name,
$dateOfBirth: dateOfBirth,
$biography: biography,
$isCurrentlyEmployed: isCurrentlyEmployed
};
db.run(sql, values, function(error) {
if (error) {
next(error);
} else {
db.get(`SELECT * FROM Artist WHERE Artist.id = ${this.lastID}`,
(error, artist) => {
res.status(201).json({artist: artist});
});
}
});
});
////////////////////////////PUT///////////////////////////////////
artistsRouter.put('/:artistId', (req, res, next) => {
const name = req.body.artist.name;
const dateOfBirth = req.body.artist.dateOfBirth;
const biography = req.body.artist.biography;
const isCurrentlyEmployed = req.body.artist.is_currently_employed === 0 ? 0 : 1;
if (!name || !dateOfBirth || !biography) {
return res.sendStatus(400);
}
const sql = (`UPDATE Artist SET name = $name, date_of_birth = $dateOfBirth,
biography = $biography, is_currently_employed = $isCurrentlyEmployed
WHERE Artist.id = $artistId`);
const values = {
$name: name,
$dateOfBirth: dateOfBirth,
$biography: biography,
$isCurrentlyEmployed: isCurrentlyEmployed,
$artistId: req.params.artistId
};
db.run(sql, values, function(error) {
if (error) {
next(error);
} else {
db.get(`SELECT * FROM Artist WHERE Artist.id = ${req.params.artistId}`,
(error, artist) => {
res.status(200).json({artist: artist});
});
}
});
});
/////////////////////////////DELETE/////////////////////////////////////////
artistsRouter.delete('/:artistId', (req, res, next) => {
const sql = (`UPDATE Artist SET is_currently_employed = 0
WHERE Artist.id = $artistId`);
const value = {$artistId: req.params.artistId};
db.run(sql, value, function (error) {
if(error) {
next(error);
} else {
db.get(`SELECT * FROM Artist WHERE Artist.id = ${req.params.artistId}`,
(error, artist) => {
res.status(200).json({artist: artist});
});
}
});
});
//////////////////////////////////////////////////////////////////////////////
module.exports = artistsRouter;
migration.js
//jshint esversion:6
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('./database.sqlite');
db.serialize(() => {
db.run(`DROP TABLE IF EXISTS Artist`);
db.run(`CREATE TABLE IF NOT EXISTS Artist
(id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
date_of_birth TEXT NOT NULL,
biography TEXT NOT NULL,
is_currently_employed INTEGER NOT NULL DEFAULT 1
)`);
});