Skip to content

Alexis Reyes — Sprint-Challenge-Mongo#255

Open
frankfaustino wants to merge 4 commits intobloominstituteoftechnology:masterfrom
CSPT1-TEAMS:CPST1-alexis-reyes-SC-mongo
Open

Alexis Reyes — Sprint-Challenge-Mongo#255
frankfaustino wants to merge 4 commits intobloominstituteoftechnology:masterfrom
CSPT1-TEAMS:CPST1-alexis-reyes-SC-mongo

Conversation

@frankfaustino
Copy link

No description provided.

@@ -0,0 +1,27 @@
const mongoose = require('mongoose');
const ObjectId = mongoose.Schema.Types.ObjectId;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use object destructuring here: const { ObjectId } = mongoose.Schema.Types

res.status(500).json({ err: 'Expense not found.'});
})
})
.post((req, res) => {
Copy link
Author

@frankfaustino frankfaustino Jun 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't need to be indented 😁

@frankfaustino
Copy link
Author

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! 💪

@frankfaustino
Copy link
Author

frankfaustino commented Jun 15, 2018

By the way, you should be answering the questions and saving them in Answers.md. They're for your benefit! You'll get asked these questions in interviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants