X-Press Publishing - help me get unstuck!

I’ve wasted the morning trying to debug this one: the test suite keeps failing my second GET (/:artistsId) route and I can’t work out why! If someone could either point me in the right direction or point out the glaring syntax error that I’ve missed it would be hugely appreciated so I can crack on with this project:

Steps 13, 14, and 15 in the test suite are failing with this error message:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves.

Everything else seems to have worked up until this point, and if I run node server.js it executes with no errors.

Code in my artists.js file:

const express = require('express')
const sqlite3 = require('sqlite3')

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

artistsRouter.param('artistId', (req, res, next, artistId) => {
    db.get(`SELECT * FROM Artist WHERE id = $artist_id`), 
    {$artist_id: artistId}, 
    (err, artist) => {
        if (err) {
            next(err)
        } else if (artist) {
            req.artist = artist; 
            next()
        } else {
            res.status(404).send()
        }

        
        
    }
});

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



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


module.exports = artistsRouter

Thanks!

I did not use a router.param here and it seems to result in some issues. Not sure why but you can try to comment out the router.param function and modify the ‘/:artistId’ route to handle the parameters. Something like this and do something similar with put and delete route handlers.

artistsRouter.get('/:artistId', (req, res, next) => {
    db.get(`SELECT * FROM Artist WHERE Artist.id = $id`,
    { $id: req.params.artistId }, (err, row) => {
        if (row) {
            res.status(200).json({artist: row});
        } else {
            res.sendStatus(404);
        };
    });
});```

The issue with the original code is this misplaced parenthesis.

The other arguments after this are never passed to db.get(), including the callback that allows it to continue to the next middleware or sends an error to the client. This ultimately results in a timeout.

2 Likes

Thanks guys. In going to carry out Mike’s suggestion of getting rid of the router.param I noticed the misplaced parenthesis. Once I sorted this out the test suite’s error messages become more helpful in debugging the rest of the code (my PUT and DELETE handlers weren’t passing either), now passed all the tests for the code I’ve written so far.

I kept the router.param in the end even though I agree it might not strictly be necessary when it’s only being used twice, unless it appears later on in the exercise.

Onwards, then!

2 Likes