Expresso and x-press Projects: Different SQL structure, different outcome?

https://www.codecademy.com/paths/create-a-back-end-app-with-javascript/tracks/back-end-development-capstones/modules/bapi-capstones-solution-code-and-next-steps/projects/x-press-publishing
https://www.codecademy.com/paths/create-a-back-end-app-with-javascript/tracks/back-end-development-capstones/modules/bapi-capstones-solution-code-and-next-steps/informationals/expresso

On both capstone projects (links above!) to the Back-End Development path, the solution had a distinct way of building the SQL query:
sql = ‘select * from db’
values = ‘const a = req.body.a’
I’ve seen that before. Besides that inside the express.js commands, it ran, from what I’ve learned, in parallel, and not inside each other.
for example, instead of (first block), it would be (second block):

Block 1

menuItems.put('/:menuItemId', function(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;
    db.get('SELECT * FROM Menu WHERE Menu.id = $menuId', {$menuId: menuId}, function(error, menu){
        if (error){
            next(error);
        } else {
            if (!name || !inventory || !price || !menu) {
                res.sendStatus(400);
            } else {
                db.run('UPDATE MenuItem SET name = $name, description = $description, invetory=$inventory, price=$price, menu_id = $menuId WHERE MenuItem.id = $menuItemId', {$name: name, $description: description, $inventory: inventory, $price: price, $menuId: menuId, $menuItemId: req.params.menuItemId}, function(error){
                    if (error){
                        next(error);
                    } else {
                        db.get('SELECT * FROM MenuItem WHERE MenuItem.id = $menuItemId', {$menuItemId: req.params.menuItemId}, function(error, menuItem){
                            res.status(200).json({menuItem: menuItem}) 
                        })  
                    }
                })
            }
        }
    })
});

Block 2

menuItemsRouter.put('/:menuItemId', (req, res, next) => {
  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) {
          res.sendStatus(400);
      }

      const sql = 'UPDATE MenuItem SET name = $name, description = $description, ' +
          'inventory = $inventory, price = $price, menu_id = $menuId ' +
          'WHERE MenuItem.id = $menuItemId';
      const values = {
        $name: name,
        $description: description,
        $inventory: inventory,
        $price: price,
        $menuId: menuId,
        $menuItemId: req.params.menuItemId
      };

      db.run(sql, values, function(error) {
        if (error) {
          next(error);
        } else {
          db.get(`SELECT * FROM MenuItem WHERE MenuItem.id = ${req.params.menuItemId}`,
            (error, menuItem) => {
              res.status(200).json({menuItem: menuItem});
            });
        }
      });
    }
  });
});

Right now I’m unable to finish this project and it seems like it’s this feature. Can someone explain it to me or find the error on my code (it’s the first block :wink: )

Thanks for your attention!
Lucas

I used this to compare your code to the solution and you have a typo in your final db.run

invetory=$inventory

I hope that helps

1 Like