'Express JS TypeError: Cannot read properties of undefined (reading 'id')

I am trying to build a database query with express js to get a recipe from a table that joins a recipe's information with specific ingredients from a third table.

The query I have works fine with pgadmin and postman when I am not specifying the id from my recipes table on which the join occurs but when I try and specify the recipe.id to return just one recipe and ingredients I get this error:

TypeError: Cannot read properties of undefined (reading 'id')
    at /Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/index.js:112:25
    at Layer.handle [as handle_request] (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/route.js:144:13)
    at Route.dispatch (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/route.js:114:3)
    at Layer.handle [as handle_request] (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/layer.js:95:5)
    at /Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/index.js:280:10)
    at /Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/index.js:30:3
    at Layer.handle [as handle_request] (/Users/x/Desktop/Programming/recipes/recipes/backend/node_postgres/node_modules/express/lib/router/layer.js:95:5)

Here is the express js query:

app.get('/getjoinedrecipes', (req, res) => {
  console.log(req, res)
  pool
    .query(
      'select * from ingredientrecipes inner join recipes on ingredientrecipes.recipe_id = recipes.id inner join ingredients on ingredientrecipes.ingredient_id = ingredients.id',
      [req.body.recipes.id]
    )
    .then((result) => {
      res.status(200).send(result.rows)
    })
    .catch((error) => {
      console.log(error)
      res.status(500).send(error)
    })
})

Here is the body I am sending to http://localhost:3001/getjoinedrecipes via postman for testing:

{
    "id": 8
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source