What's wrong with my code?

db.run(sql, values, function(error) {
        if (error) {
            next(error);
        } else {
          db.get(`SELECT * FROM Menu WHERE Menu.id = ${this.lastID}`,
            (error, menu) => {
              res.status(201).json({menu: menu});
            });
        }
    });

    db.run(sql, values, function(error) {
    if (error) {
      next(error);
    } else {
      db.get(`SELECT * FROM Menu WHERE Menu.id = ${this.lastID}`,
        (error, menu) => {
          res.status(201).json({menu: menu});
        });
    }
  });

this is the espresso exercise. the top is the code i wrote. the bottom is the answer code from codecademy. the bottom will pass the test. mine will not. i can’t figure it out. why!?!?!?!?!?!

Hi,

Welcome to the forum. I’ve gone over them both a few times and they look to be identical. When you say the solution code works, is that when you paste it into your own code or when you use it as part of the entire solution code package?

It may be that something further up in your code is the difference between passing an failing the test. Given the use of a non arrow function I’m guessing you’re code is from a POST route to the Menu table.

If you can post the whole code segment for that route and tell me what part of the test is it failing I’ll have a look for you.

thank you for your response. thanks for trying to help me! basically the post portion it says the newly created should be returned. it fails on my code but passes on codecademy’s code.

const express = require('express')

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

const sqlite3 = require('sqlite3')
const db = new sqlite3.Database(process.env.TEST_DATABASE || './database.sqlite');
/*
menuItemsRouter.param('menuItemId',(req,res,next,menuItemId)=>{
    const sql = 'SELECT * FROM MenuItem WHERE MenuItem.id = $menuItemId'
    const values ={$menuItemId: menuItemId}
    db.get(sql,values,(error,menuItem)=>{
        if (error){
            next(error)
        } else if (menuItem){
            next();
        } else {
            res.sendStatus(404)
        }
    })
}) */
menuItemsRouter.param('menuItemId', (req, res, next, menuItemId) => {
    const sql = 'SELECT * FROM MenuItem WHERE MenuItem.id = $menuItemId';
    const values = {$menuItemId: menuItemId};
    db.get(sql, values, (error, menuItem) => {
      if (error) {
        next(error);
      } else if (menuItem) {
        next();
      } else {
        res.sendStatus(404);
      }
    });
  });

menuItemsRouter.get('/',(req,res,next)=>{
    const sql = 'SELECT * FROM MenuItem WHERE MenuItem.menu_id=$menuId'
    const values ={$menuId: req.params.menuId}

    db.all(sql,values,(error,menuItems)=>{
        if (error){
            next(error)
        } else {
            res.status(200).json({menuItems: menuItems})
        }
    });        
});
/* my post code
menuItemsRouter.post('/',(req,res,next)=>{
    const name = req.body.menuItem.name;
    const description = req.body.menuItem.description;
    const inventory = req.body.menuItem.inventory;
    const price = req.body.menuItem.price;
    const menuId = req.params.menuId;
    const menuSql = 'SELECT * FROM Menu WHERE Menu.id = $menuId';
    const menuValues = {$menuId: menuId};
    db.get(menuSql,menuValues,(error,menu)=>{
        if (error){
            next(error);
        } else {
            if (!name|| !inventory || !price || !menu) {
                return res.sendStatus(400);
            }
        
            const sql = 'INSERT INTO MenuItem (name, description, inventory, price, menu_id)' +
            'VALUES ($name, $description, $inventory, $price, $menuId)';
            
            const values = {
                $name: name,
                $description: description,
                $inventory: inventory,
                $price: price,
                $menuId: menuId };
        
            db.run(sql,values,function(error){
                if(error){
                    next(error);
                } else {
                    db.get(`SELECT * FROM MenuItem WHERE MenuItem.id=${this.lastId}`,
                    (error, menuItem)=> {
                            if (error) {
                                next(error);
                            } else {
                                res.status(201).json({ menuItem: menuItem });
                            }
                        });
                }
            });

        }
    });
    
});
*/

menuItemsRouter.post('/', (req, res, next) => { //codecademy's code
    const name = req.body.menuItem.name,
          description = req.body.menuItem.description,
          inventory = req.body.menuItem.inventory,
          price = req.body.menuItem.price,
          menuId = req.params.menuId;
    const menuSql = 'SELECT * FROM Menu WHERE Menu.id = $menuId';
    const menuValues = {$menuId: menuId};
    db.get(menuSql, menuValues, (error, menu) => {
      if (error) {
        next(error);
      } else {
        if (!name || !inventory || !price || !menu) {
          return res.sendStatus(400);
        }
  
        const sql = 'INSERT INTO MenuItem (name, description, inventory, price, menu_id)' +
            'VALUES ($name, $description, $inventory, $price, $menuId)';
        const values = {
          $name: name,
          $description: description,
          $inventory: inventory,
          $price: price,
          $menuId: menuId
        };
  
        db.run(sql, values, function(error) {
          if (error) {
            next(error);
          } else {
            db.get(`SELECT * FROM MenuItem WHERE MenuItem.id = ${this.lastID}`,
              (error, menuItem) => {
                res.status(201).json({menuItem: menuItem});
              });
          }
        });
      }
    });
  });

menuItemsRouter.put('/:menuItemId',(req,res,next)=>{
    const name = req.body.menuItem.name;
    const description = req.body.menuItem.description;
    const inventory = req.body.menuItem.inventory;
    const price = req.body.menuItem.price;
    const menuItemId = req.params.menuItemId;
    const menuId = req.params.menuId
    const menuSql = 'SELECT * FROM Menu WHERE Menu.id = $menuId';
    const menuValues = {$menuId: menuId};
    db.get(menuSql,menuValues,
    (error,menu)=>{
        if (error){
            next(error);
        } else {
            if (!name|| !inventory || !price || !menu) {
                return res.sendStatus(400);
            }
            const updateSql = 'UPDATE MenuItem SET name=$name, description=$description, inventory=$inventory, price=$price WHERE MenuItem.id = $menuItemId';
            const updateValues = {
                $name: name,
                $description: description,
                $inventory: inventory,
                $price: price,
                $menuItemId: menuItemId
            };
            db.run(updateSql,updateValues,(error)=>{
                if (error){
                    next(error);
                } else {
                    db.get(`SELECT * FROM MenuItem WHERE MenuItem.id=${req.params.menuItemId}`, 
                    (error,menuItem)=>{
                        if (error){
                            next(error);
                        } else {
                            res.status(200).json({menuItem: menuItem});
                        }
                    })
                }
            })
        }
    })

})

menuItemsRouter.delete('/:menuItemId', (req, res,next)=>{
    const sql = 'DELETE FROM MenuItem WHERE MenuItem.id=$menuItemId';
    const values = {$menuItemId: req.params.menuItemId};

    db.run(sql,values, (error)=>{
        if(error){
            next(error);
        } else {
            res.sendStatus(204);
        }
    })
})


module.exports= menuItemsRouter;

No worries.

Take a look at the following and see if you can figure it out.

In your POST route code, look at the SQL query to get the new menuItem. Pay particular attention to WHERE id=${this.Id} What would stop that bit of code from finding anything?

all this time. it was as simple as capitalizing the D in this.lastID. i’m incredibly annoyed and relieved at the same time. thank you for helping me.

if i can ask, how do you find these tiny mistakes? i must have looked at the line of code a million times and even when you showed me where to look i couldn’t figure it out until I noticed the lower case d.

1 Like

You’re welcome. I’m just a few lessons ahead of you in the web path so I feel your pain, I’ve spent a lot of time looking for typos!

With regards to finding mistakes, I try to follow the clues and then break the problem down into small logical steps. I read through your whole code block first and nothing jumped out at me. I didn’t see it until I broke it down.

In this case you said that the only test it was failing was to return the new item. From this I concluded that your code was sending the 201 status just fine and so it was mainly a problem concerning the menuItem variable.

The .json in the res message looked fine so next step up the chain was the db.get code. Focusing on that revealed the problem.

2 Likes

Thank you, winningblue. You made my day. I think I love coding even more because of helpful people like you. :slight_smile:

1 Like