Cumulative API projects

Hey guys, anyone done the Cumulative API projects yet? Expresso in this case. This is the link
https://www.codecademy.com/paths/web-development/tracks/building-a-persistent-api/modules/persistent-api-cumulative-projects/informationals/cumulative-project-expresso

I have passed all the CRUD tests but I get a SQLITE CONSTRAINT ERROR: NOT NULL constraint failed. Employee.name.

This is my code below

const express = require(‘express’);

const employeesRouter = express.Router();

const timeSheetsRouter = require(’./timesheets’);

const sqlite3 = require(‘sqlite3’);

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

employeesRouter.param(‘employeeId’, (req, res, next, employeeId) => {

db.get('SELECT * FROM Employee WHERE Employee.id = $employeeId',

{

    $employeeId : employeeId

}, (error, employee) => {

    if(error) {

        next(error)

    } else if(employee) {

        req.employee = employee;

        next()

    } else {

        res.status(404).send()

    }

}

)

});

employeesRouter.use(’/:employeeId/timesheets’, timeSheetsRouter);

employeesRouter.get(’/’, (req, res, next) => {

db.all('SELECT * FROM Employee WHERE Employee.is_current_employee = 1', 

(err, employees) => {

    if(err) {

        next(err)

    } else {

        res.status(200).json({ employees : employees })

    }

}

)

});

employeesRouter.get(’/:employeeId’, (req, res, next) => {

res.status(200).json({employee : req.employee})

});

employeesRouter.post(’/’, (req, res, next) => {

const name = req.body.employee.name;

const position = req.body.employee.position;

const wage = req.body.employee.wage;

const isCurrentEmployee = req.body.employee.isCurrentEmployee === 0 ? 0 : 1;

if(!name || !position || !wage) {

    res.sendStatus(400)

}

db.run(`INSERT INTO Employee (name, position, wage, is_current_employee) VALUES ($name, $position, $wage, $isCurrentEmployee)`, {

    $name : name,

    $position : position,

    $wage : wage,

    $isCurrentEmployee : isCurrentEmployee

},

function(error) {

    if(error) {

        next(error)

    } else {

        db.get(`SELECT * FROM Employee WHERE id = ${this.lastID}`, (error, employee) => {

            res.status(201).json({employee : employee})

        })

    }

}

)

});

employeesRouter.put(’/:employeeId’, (req, res, next) => {

const name = req.body.employee.name,

      position = req.body.employee.position,

      wage = req.body.employee.wage,

      isCurrentEmployee = req.body.employee.isCurrentEmployee === 0 ? 0 : 1;

if(!name || !position || !wage) {

    return res.sendStatus(400);

}

db.run('UPDATE Employee SET name = $name, position = $position, wage = $wage, is_current_employee = $isCurrentEmployee WHERE Employee.id = $employeeId', 

{

    $name : name,

    $position : position,

    $wage : wage,

    $isCurrentEmployee : isCurrentEmployee,

    $employeeId : req.params.employeeId

},

(error) => {

    if(error) {

        next(error)

    } else {

        db.get(`SELECT * FROM Employee WHERE Employee.id = ${req.params.employeeId}`,

        (error, employee) => {

            res.status(200).json({ employee : employee})

        });

    };

}

)

});

employeesRouter.delete(’/:employeeId’, (req, res, next) => {

db.run('UPDATE Employee SET is_current_employee = 0 WHERE Employee.id = $employeeId', {

    $employeeId : req.params.employeeId

}, (error) => {

    if(error) { next(error) } else {

        db.get(`SELECT * FROM Employee WHERE Employee.id = ${req.params.employeeId}`, 

        (error, employee) => {

            res.status(200).json({employee : employee})

        })

    }

}

);

});

module.exports = employeesRouter;

In this route you didn’t use an ‘else’ statement so try adding ‘return’ before res.sendStatus(400) as you did with your PUT route. Also look at your DELETE route.

Thanks @chrisandrew_dev and @ainederrick.

Everyone else, if a simple return; does not work for you, try return next();. If I understand this correctly, adding next with the return statement after the 400 status code should skip the logic in your route handler and send the request down the middleware chain.

Adding return next(); after sending the 400 status code helped remove the “Error: SQLITE_CONSTRAINT: NOT NULL constraint failed: Employee.name” error message in my testing suite.

Helpful resources:

Hope this helps.

Cheers!