Jesse Reichel — Sprint-Challenge-Mongo#258
Jesse Reichel — Sprint-Challenge-Mongo#258frankfaustino wants to merge 3 commits intobloominstituteoftechnology:masterfrom
Conversation
| @@ -0,0 +1,15 @@ | |||
| 1. Describe the following: `DataBase`, `Collection` , `Document` | |||
| DataBase: Is a large assortment of Collections and Documents; it represents the largest group in the mongo file structure. | |||
| const router = express.Router(); | ||
|
|
||
| router.post('/', (req, res) => { | ||
| const {title, budgetAmount} = req.body |
There was a problem hiding this comment.
Since we're not using the variables title and budgetAmount for anything, we could just pass req.body to our new Budget: const budget = new Budget(req.body) and skip the whole object destructuring thing.
| }) | ||
|
|
||
| router.post('/', (req, res) => { | ||
| const {title } = req.body |
There was a problem hiding this comment.
Character spacing here is a bit off. According to Airbnb's styleguide (https://github.com/airbnb/javascript#whitespace--in-braces), there should be spaces inside curly braces.
Also, we don't need to use object destructuring here.
| const Expense = new mongoose.Schema({ | ||
| amount: Number, | ||
| description: String, | ||
| budget:[{ |
There was a problem hiding this comment.
Since Expenses are many-to-one relationship with Budgets, this budget property shouldn't be an array.
| type: ObjectId, | ||
| ref: "Budget" | ||
| }], | ||
| category:[{ |
There was a problem hiding this comment.
Same here, there are many Expenses per Category, but only one Category for each, so this property shouldn't be in an array.
| }) | ||
| }) | ||
|
|
||
| router.post('/', (req, res) => { |
There was a problem hiding this comment.
Instead of sending the budget and category ObjectId along with the POST request, we can send a POST request that looks like this:
{
"amount": 35,
"description": "potatoes",
"budget": "Monthly Spending",
"category": "Groceries"
}In order to do that, we need to add some logic to our route:
.post((req, res) => {
let { amount, budget, category, description } = req.body
budget = Budget.findOne({ title: budget })
category = Category.findOne({ title: category })
Promise.all([budget, category])
.then(([budget_id, category_id]) => {
const expense = new Expense({
amount,
budget: budget_id,
category: category_id,
description
})
expense.save()
.then(response => res.status(201).json(response))
.catch(err => res.status(500).json(err)))
})
.catch(err => res.status(500).json(err))
})We have to get budget and category with the findOne method, then using Promise.all, we grab the ObjectId for each from the response from MongoDB, then create our new Expense.
|
Well done, Jesse! You've clearly got a good grasp of the material from last week. I know it wasn't clear in the assignment's instructions that you have to fetch the budget and category Otherwise, good job! Keep up the excellent work 😊 |
No description provided.