X-Press Publishing Cannot Post, GET, PUT or DELETE

Anyone else who’s done this project have this problem?
Tables creation works perfectly and I can see them in DB browse, but the CRUD operations all fail. POST will fail with error 404. I have followed instructions very closely.
What am I oding wrong??

Hello @bilbo246866, Welcome to the forums!!

Can you please post your code, and a link to the project so people can better assist you.
Currently I don’t have your code so there is no way for me to see what you are doing wrong.
Thank you :smiley:

1 Like

I will do so once I figure out how to post code on this site. (new on here).

You can use the </> button at the top of the screen, to format code.
To add a link just copy and paste the url from your web browser.

Should you need anything else let me know.

  1. Here is the URL for the project on Codecademy:

https://www.codecademy.com/paths/web-development/tracks/building-a-persistent-api/modules/persistent-api-cumulative-projects/projects/x-press-publishing

  1. artist.js (Route handlers for Artists for CRUD) We’ll start with that!
const app = express();
const artistsRouter = express.Router();
const sqlite3 = require('sqlite3');

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

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;
			next();
		} else {
			res.sendStatus(404); //not found
		}

	});
}); 

artistsRouter.get('/', (req, res, next)=> {
	db.all('SELECT * FROM Artist WHERE Artist.is_currently_employed = 1', (err, artists)=> {
		if(err){
			next(err);
		} else {
			res.status(200).json({artists: artists});
		}
	
  });

});	

artistsRouter.get('/:artistId', (req, res, next)=> {
	res.sendStatus(200).json({artist: req.artist})
});	

artistsRouter.POST('/', (req, res, next)=> {
	const name = req.body.artist.name;
	const dateOfBirth = req.body.artist.dateOfBirth;
	const biography = req.body.dateOfBirth;
	const isCurrentlyEmployed = req.body.artist.isCurrentlyEmployed === 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: is_currently_employed
};

db.run(sql, values, function(error) {
	if(error){
	next(error);
	} else {
		db.get(`SELECT * FROM Artist WERE Artist.id = ${this.lastID}`, (error, artist)=>{
			res.sendStatus(201).json({artist: artist});
	}); 

    }

  });

}); //End POST

artistsRouter.put('/:artistId', (req, res, next, artist)=> {
	const name = req.body.artist.name;
	const dateOfBirth = req.body.artist.dateOfBirth;
	const biography = req.body.dateOfBirth;
	const isCurrentlyEmployed = req.body.artist.isCurrentlyEmployed === 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.send(200).json({artist: artist});
			});
		}
	});

});

artistsRouter.delete('/artistId', (req, res, next )=> {
	const sql = 'UPDATE SET Artist is_currently_employed = 0 WHERE Artist.id = $artistId';
	const values = {$artistId = req.params.artistId};

	db.run(sql, values, (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;
  1. Copy of server.js file
const express = require('express');
const bodyParser = require('body-parser');
const errorhandler = require('errorhandler');
const cors = require('cors');
const morgan = require('morgan');
const apiRouter = require('./api/api');


const PORT = process.env.PORT || 4000;
const app = express();

app.use(bodyParser.json());
app.use(morgan('dev'));

app.use(cors());
app.use('/api', apiRouter);
app.use(errorhandler());

app.listen(PORT, ()=>{
	console.log(`Listening on port ${PORT}...`);
});

module.exports = app;
  1. Copy of migration.js file (Creates the Tables in the db file. This part actually works!).
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('./database.sqlite');

db.serialize(function() {

db.run('CREATE TABLE IF NOT EXISTS `Artist` ( ' +
    '`id` INTEGER NOT NULL, ' +
    '`name` TEXT NOT NULL, ' +
    '`date_of_birth` TEXT NOT NULL, ' +
    '`biography` TEXT NOT NULL, ' +
    '`is_currently_employed` INTEGER NOT NULL DEFAULT 1, ' +
    'PRIMARY KEY(`id`) )');

    db.run('CREATE TABLE IF NOT EXISTS `Series` ( ' +
    	'`id` INTEGER NOT NULL, ' +
    	'`name` TEXT NOT NULL, ' +
    	'`description` TEXT NOT NULL, ' +
    	'PRIMARY KEY(`id`) )');

    db.run('CREATE TABLE IF NOT EXISTS `Issue` (' +
    	'`id` INTEGER NOT NULL,' +
    	 '`name` TEXT NOT NULL,' +
    	 '`issue_number` INTEGER NOT NULL,' +
    	 '`publication_date` TEXT NOT NULL,' +
    	 '`artist_id` INTEGER NOT NULL,' +
    	 '`series_id` INTEGER NOT NULL,' +
    	 'PRIMARY KEY(`id`), ' +
    	 'FOREIGN KEY(`artist_id`) REFERENCES `Artist`(`id`),' +
    	 'FOREIGN KEY(`series_id`) REFERENCES `Series`(`id`) )' );
});

Looks like you haven’t imported Express into your artists.js file.

your first line is:
const app = express();

I think you need to change that to:
const express = require('express');

Hope that helps!

Hi, Thanks, but that line, const express = require(‘express’) is part of my code and on the 1st line of the artists.js file. It just somehow didn’t get copied when I copy/pasted the selected code to you.
The problem doesn’t seem obvious. I’m going to try logging and see where it may fail in the console.

What does your api.js file look like?

const express = require('express');

const apiRouter =express.Router();
const artistsRouter = express.Router('./artists.js');
const seriesRouter = express.Router('./series.js');


apiRouter.use('/artists', artistsRouter);
apiRouter.use('/series', seriesRouter);
module.exports = apiRouter;

When you are declaring your artistsRouter and seriesRouter in app.js it should be like this:

const artistsRouter = require('./artists.js');
const seriesRouter = require('./series.js');

What you have right now is:

const artistsRouter = express.Router('./artists.js');
const seriesRouter = express.Router('./series.js');

const artistsRouter = express.Router(); is what you put in your artists.js file to create the router but when using it in api.js you import it with const artistsRouter = require('./artists.js');

I just finished this project and this is my understanding. Let me know if I’m wrong!

You’re not wrong. You’ve set me on the right path now. That was the problem.
Still need to do more work on this to get all the tests to pass, but I’m further ahead now, and at least I can create Series and Artists.