Codestips post error

I am working my way through the Web Development path and I am having issues with the Codestrips project. And the first time I ran the tests, everything passed. When I tried to input text in he app, I could not input anything. I checked all my code with the walkthrough and made sure everything was correct and then ran tests again and now I am failing the post tests:

  1. POST /strips route should create a valid strip:
    Uncaught Error: Can’t set headers after they are sent.
    at ServerResponse.setHeader (_http_outgoing.js:371:11)
    at ServerResponse.header (node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (node_modules/express/lib/response.js:267:15)
    at ServerResponse.send (node_modules/express/lib/response.js:158:21)
    at Statement.db.get (app.js:59:29)

  2. POST /strips route should return a 201 status code after strip creation:
    Uncaught Error: Can’t set headers after they are sent.
    at ServerResponse.setHeader (_http_outgoing.js:371:11)
    at ServerResponse.header (node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (node_modules/express/lib/response.js:267:15)
    at ServerResponse.send (node_modules/express/lib/response.js:158:21)
    at Statement.db.get (app.js:59:29)

I have even copied and pasted the code solution and the tests still fail and I am not able to type in the text bubbles. Here is my validation middleware and post request:

const validateStrip = (req, res, next) => {
const stripToCreate = req.body.strip;
if (
!stripToCreate.head ||
!stripToCreate.body ||
!stripToCreate.background ||
!stripToCreate.bubbleType
) {
return res.sendStatus(400);
}
next();
}

app.post(’/strips’, validateStrip, (req, res, next) => {
const stripToCreate = req.body.strip;
db.run(
INSERT INTO Strip (head, body, background, bubble_type, bubble_text, caption) VALUES ($head, $body, $background, $bubbleType, $bubbleText, $caption),
{
$head: stripToCreate.head,
$body: stripToCreate.body,
$background: stripToCreate.background,
$bubbleType: stripToCreate.bubbleType,
$bubbleText: stripToCreate.bubbleText,
caption: stripToCreate.caption }, function(err) { if (err) { return res.sendStatus(500); } db.get(`SELECT * FROM Strip WHERE id = {this.lastID}`, (err, row) => {
if (!row) {
return res.sendStatus(500);
}
res.sendStatus(201).send({ strip: row });
})
}
)
})

If anyone could point me in the right direction it would be much appreciated!

Hello, it’s a little difficult to follow because of how the code is formatted on the forum.

One thing that stands out to me is

res.sendStatus(201).send({ strip: row });

On this line, try setting the status code of the response with status() instead of using sendStatus(). This way the status of the response will have the code you want, but it won’t be sent back to the client until the rest of the data has been attached.

If this isn’t it or if you need help in the future, be sure to post the code in a ‘Preformatted text’ block
image
That way the extra whitespace doesn’t get removed and there can be some keyword highlighting.

1 Like

Thank you for the response! Changing sendStatus() to status() did allow me to pass all the tests, however I am still unable to edit the text in code strip.

(https://www.codecademy.com/paths/web-development/tracks/building-a-persistent-api/modules/persistent-api-cumulative-projects/projects/codestrips-prj)

here is my full code:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const morgan = require('morgan');
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database(process.env.TEST_DATABASE || './db.sqlite');

const PORT = process.env.PORT || 4001;

app.use(express.static('public'));
app.use(morgan('dev'));
app.use(bodyParser.json());

app.get('/strips', (req, res, next) => {
  // double check to make sure that the table is Strip not Strips
  db.all('SELECT * FROM Strip', (err, rows) => {
    if (err) {
      res.sendStatus(500);
    } else {
      res.send({ strips: rows });
    }
  });
});

const validateStrip = (req, res, next) => {
  const stripToCreate = req.body.strip;
  if (
    !stripToCreate.head ||
    !stripToCreate.body ||
    !stripToCreate.background ||
    !stripToCreate.bubbleType
  ) {
    return res.sendStatus(400);
  }
  next();
}

app.post('/strips', validateStrip, (req, res, next) => {
  const stripToCreate = req.body.strip;
  db.run(
  `INSERT INTO Strip (head, body, background, bubble_type, bubble_text, caption)
  VALUES ($head, $body, $background, $bubbleType, $bubbleText, $caption)`,
    {
      $head: stripToCreate.head,
      $body: stripToCreate.body,
      $background: stripToCreate.background,
      $bubbleType: stripToCreate.bubbleType,
      $bubbleText: stripToCreate.bubbleText,
      $caption: stripToCreate.caption
    },
    function(err) {
      if (err) {
        return res.sendStatus(500);
      }
      db.get(`SELECT * FROM Strip WHERE id = ${this.lastID}`, (err, row) => {
        if (!row) {
          return res.sendStatus(500);
        }
        res.status(201).send({ strip: row });
      }) 
    }
  )
})

app.listen(PORT, () => {
  console.log(`Server is listening on port: ${PORT}`)
});

module.exports = app;

I’m a little conflicted because I am eager to move on to the next project because I have already spent an absurd amount of time trying to figure this out, but at the same time I don’t want to go through this again lol

Thanks!

Glad that part worked for you.

What do you mean that you can’t edit the text in the code strip? Like is it not working on the front-end or after you save it isn’t showing when you reload?

You mentioned all the tests pass, so I assume your database was set up correctly too since that’s the first set of tests.

Were you ever able to get the text editor to work. I am unable to write anything in the Text bubble as well?