X-press Publishing Step 31: "no such column: undefined" in failed test result

Hello!

I’m currently going through the back-end development path, and I reached the capstone project for it called X-Press Publishing. However I’ve reached step 31 but the test for the POST function keeps failing and gives me a “Error: SQLITE_ERROR: no such column: undefined”. I tried to watch the solution and make some corrections to my code to make it look as close as possible from the solution without being exactly the same, but I keep getting this error. Here’s the full error report for the post function:

Now here’s my code for the artist module:

const express = require('express');
const sqlite3 = require('sqlite3');
const artistsRouter = express.Router();

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

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.param('artistId',(req,res,next,artistId)=>{
    db.get(`SELECT * FROM Artist WHERE id=$artistId`,{$artistId:artistId},(err,artist)=>{
        if(err){next(err)}else if(artist){
            req.artist=artist;
        next()}else{res.sendStatus(404)}}
    )
})

artistsRouter.get('/:artistId',(req,res,next)=>{res.status(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.artist.biography;
const isCurrentlyEmployed = req.body.artist.isCurrentlyEmployed === 0 ? 0 : 1;
if(!name || !dateOfBirth || !biography){return res.sendStatus(400)}

db.run(`INSERT INTO Artist (name,date_of_birth,biography,is_currently_employed) VALUES($name,$dateOfBirth,$biography,$isCurrentlyEmployed)`,{$name:name,$dateOfBirth: dateOfBirth,$biography: biography,$isCurrentlyEmployed: isCurrentlyEmployed},
(error)=>{
if(error){next(error)}else{
    db.get(
        `SELECT * FROM Artist WHERE Artist.id=${this.lastID}`,
        (error,artist)=>{if(error){next(error)}else(res.status(201).json({artist : artist}))}
        )
}
})
})

module.exports = artistsRouter;```

Until my POST request, the code was functioning well (for the get request).

I really don't see what's causing my test to fail, could someone help me with it?

Thank you very much in advance for any help!

Hello, and welcome back to the forums

You’re currently passing an arrow function as the callback to db.run(), so you won’t have access to this.lastID when you try to retrieve the new record. db.run() attaches the lastID property to this within the scope of the callback for INSERT statements, Since it doesn’t have access to the same this in your arrow function callback, this.lastID is undefined.

If you convert your arrow function to an anonymous function, then you should be good with the rest of the code.

e.g.

(error) => {
  //...
}

function(error) {
  //...
}

Hi!
Thank you very much, it worked perfectly indeed! ^^

Is there somewhere where I can read more about why an arrow function doesn’t allow for access to the “this.”?
I remember it was mentioned during the lesson, but I didn’t quite understand why…

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