Alexis Reyes — Sprint-Challenge-Mongo#255
Alexis Reyes — Sprint-Challenge-Mongo#255frankfaustino wants to merge 4 commits intobloominstituteoftechnology:masterfrom
Conversation
| @@ -0,0 +1,27 @@ | |||
| const mongoose = require('mongoose'); | |||
| const ObjectId = mongoose.Schema.Types.ObjectId; | |||
There was a problem hiding this comment.
We can use object destructuring here: const { ObjectId } = mongoose.Schema.Types
| res.status(500).json({ err: 'Expense not found.'}); | ||
| }) | ||
| }) | ||
| .post((req, res) => { |
There was a problem hiding this comment.
Make sure we're fetching then saving our budget and category ObjectId to our new Expense. Below's an example of how you could go about it with Promise.all
Postman req.body:
{
"amount": 35,
"description": "potatoes",
"budget": "Monthly Spending",
"category": "Groceries"
}Expense POST route:
.post((req, res, next) => {
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 => next(err))
})
.catch(err => next(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.
| }) | ||
| }) | ||
| .post((req, res) => { | ||
| const { title, budgetAmount } = req.body; |
There was a problem hiding this comment.
Since we're not doing anything with the variables title and budgetAmount, you could omit lines 18 and 19. Then do: const budget = new Budget(req.body)
| console.log('Failed to connect to MongoDB', err); | ||
| }) | ||
|
|
||
| server.use('/budgets', budgets); |
There was a problem hiding this comment.
These don't need to be indented 😁
|
Good work on the Sprint Challenge, Alexis! I know you said you got hung up on saving the budget and category refs to a new Expense. I've added an example of how to do just that in the comments. Please review the comments, then close your PR. Keep up the great work! 💪 |
|
By the way, you should be answering the questions and saving them in |
No description provided.