We need to add a new tree record to our database via Sequelize’s create method. We also must handle:
req.body to get name, location, height, and size.create on the Tree model with correct field mapping.File Name & Location: server/routes/trees.js
// Basic pseudocode for POST /trees:
// 1. Extract name, location, height, size from req.body
// 2. Use Tree.create({ tree: name, location, heightFt: height, groundCircumferenceFt: size })
// 3. Respond with JSON containing newly created record
// 4. If error (e.g., duplicate), respond with error JSON
Expected Input: A POST request body with valid fields (name, location, height, size).
Expected Output: JSON with the new tree’s details or an error message if invalid.
Step-by-Step Directions:
trees.js and locate the POST route.req.body.tree to name, heightFt to height, etc.Tree.create(...) with the mapped data.Explanations & Analogies: create is like filling out a new library card for a book and inserting it into the system. If the book’s title already exists, the system complains.
Real World Example: A user inputs details about a newly discovered tree in a nature preserve.
Further Examples: Validation (all text fields exist, numeric fields in correct range) to reduce data errors.
Check if newly inserted tree records appear in subsequent GET requests. Confirm that a second insertion of the same name is handled gracefully.