X-Press publishing step 51

that is my code but i don’t know why there is an sqlite error, help please

On line 87-88 you have a multi-line SQL statement. It looks like you have a quotation mark before WHERE on the second line. I usually use backticks for the multi-line strings as it solves some issues. And make sure to only have a quotation mark at the beginning and end of string and not in the middle…

no, that was not the error since in the first photo it was not noticeable but it had the plus sign and that is why it had more quotes

Ok. In your sql string you are using $artistId but when you assign that one in the values object you are using $artist_id. These two have to match similar to what you have with $name (for example).

thanks it worked but i still have this error:

could you help me please

On line 84-86 you are validating input. You should move this to before the SQL queries start (line 80). That way the desired error code (400) can be sent back if those values are missing. You also have !artist as one of those constants but you probably intended artistId which you are setting on line 77.

I am not sure this will fix the problem though. I recommend copying and pasting the entire route-handler code instead of screen-shot. If you do, then use the </> function of this editor for correct formatting. Currently, some of the end of the function is missing in screenshots. Being able to copy and paste the code can really help with troubleshooting…

const express = require(‘express’);

const issuesRouter = express.Router({mergeParams: true});

const sqlite3 = require(‘sqlite3’);

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

issuesRouter.param(‘issueId’, (req,res,next, issueId) => {

const sql = ‘SELECT * FROM Issue WHERE Issue.id = $issueId’;

const values = {$issueId: issueId}

db.get(sql, values, (error, issue) => {

if(error){

  next(error);

}else if(issue){

  next();

}else{

  res.sendStatus(400);

}

})

})

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

const sql = 'SELECT * FROM Issue WHERE Issue.series_id = $seriesId';

const values = {$seriesId: req.params.seriesId};

db.all(sql, values, (error, issues) => {

     if (error) {

        next(error);

      } else {

        res.status(200).json({issues: issues});

      }

})

})

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

const name = req.body.issue.name;

const issueNumber = req.body.issue.issueNumber;

const publicationDate = req.body.issue.publicationDate;

const artistId = req.body.issue.artistId;

const artistSql = ‘SELECT * FROM Artist WHERE Artist.id = $artistId’;

const artistVal = {$artistId: artistId};

db.get(artistSql, artistVal, (error, artist) => {

if(error){

  next(error);

}else{

  if(!name || !issueNumber || !publicationDate || !artist){

    return res.sendStatus(400);

  }

const sql = 'INSERT INTO Issue (name, issue_number, publication_date, artist_id, series_id)' +

  'VALUES ($name, $issueNumber, $publicationDate, $artistId, $seriesId)'; 



const values = { 

   $name: name, 

   $issueNumber: issueNumber, 

   $publicationDate: publicationDate,

   $artistId: artistId,

   $seriesId: req.params.seriesId

}; 

db.run(sql, values, function(error){

  if(error){

    next(error);

  }else{

    db.get(`SELECT * FROM Issue WHERE Issue.id = ${this.lastId}`,

    (error, issues) => {

      res.status(201).json({issues: issues});

    })

  }

})

}

})

})

issuesRouter.put(’/:issueId’, (req, res, next) => {

const name = req.body.issue.name;

const issueNumber = req.body.issue.issueNumber;

const publicationDate = req.body.issue.publicationDate;

const artistId = req.body.issue.artistId;

const artistSql = ‘SELECT * FROM Artist WHERE Artist.id = $artistId’;

const artistVal = {$artistId: artistId};

db.get(artistSql, artistVal, (error, artist) => {

if(error){

  next(error);

}else{

  if(!name || !issueNumber || !publicationDate || !artist){

    return res.sendStatus(400);

  }

  const sql = 'UPDATE Issue SET name = $name, issue_number = $issueNumber,' +

  'publication_date = $publicationDate,  artist_id = $artistId ' +

  'WHERE Issue.id = $issueId';

  const values = {

  $name: name,

  $issueNumber: issueNumber,

  $publicationDate: publicationDate,

  $artistId: artistId,

  $issueId: req.params.issueId

};

db.run(sql, values, function(error){

  if(error){

    next(error)

  }else {

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

    (error, issue) => {

      res.status(200).json({issue: issue});

    }

    )

  }

})

  

}

})

})

issuesRouter.delete(’/:issueId’, (req, res, next) => {

db.run(‘DELETE FROM Issue WHERE Issue.id = $issueId’, {$issueId: req.params.issueId}, (error) => {

if(error){

  next(error);

}else{

  res.sendStatus(204);

}

})

})

module.exports = issuesRouter;

this is the entire issue.js code

Is the problem still there? If so, do you mind editing your post or making a new one where you use </> function in this forum editor. Press that icon and paste the code in between the 2 sets of triple backticks (```). This will make sure the formatting is good and the forum-editor interprets it as code to be viewed.

1 Like

no, I already fixed it with what you told me before thanks

1 Like

but i still have 1 error:

PUT /api/series/:seriesId/issues/:issueId
√ should update the issue with the given ID (91ms)
√ should return a 200 status code after issue update (123ms)
√ should return the updated issue after issue update (102ms)
4) should return a 404 status code for invalid issue IDs // -------this
√ should return a 400 status code for invalid issue updates
√ should return a 400 status code if an artist with the updated artist ID doesn’t exist

with the code above

The test wants a 404 code for invalid issue id. The validity of the issueId is being check in your issuesRouter.param function which is called before the put handler (it will also be used for the delete route). If you look in the .param function you are sending back a 400 code currently…

1 Like

thanks i fixed everything

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.