John C — Sprint-Challenge-Mongo#257
John C — Sprint-Challenge-Mongo#257frankfaustino wants to merge 1 commit intobloominstituteoftechnology:masterfrom
Conversation
| An instance of an element or item in a collection. | ||
|
|
||
| # Q2 : Relationships in MongoDB | ||
| Relationships can be achieved in mongoDB through a ref type. To populate the document with the reference type you can do an extra db call or use a library(mongoose). |
| @@ -0,0 +1,14 @@ | |||
| const router = require('express').Router(); | |||
| const budgetdb = require('./buget') | |||
There was a problem hiding this comment.
Missing some semi-colons ; in this file.
| @@ -0,0 +1,12 @@ | |||
| const Router = require('express').Router() | |||
| const Categorydb = require('./category'); | |||
There was a problem hiding this comment.
We should be using camelCase for variable names.
| const obj = req.body; | ||
| Categorydb.create(obj,(err,newcategory) => { | ||
| if (err) res.status(500).json(err); | ||
| res.status(202).json(newcategory); |
There was a problem hiding this comment.
HTTP status code should be 201, since we're creating a new document and sending it to our client (Postman).
| const mongoose = require('mongoose') | ||
| const Schema = mongoose.Schema; | ||
|
|
||
| const ExpenseSchema = new Schema({ |
There was a problem hiding this comment.
Not to be too pedantic 😆, but for readability's sake, we should be adding spaces inside curly braces, after commas and colons:
const ExpenseSchema = new Schema({
amount: {
type: Number,
required: true
},
description: {
type: String,
required: true
},
budget: {
type: Schema.Types.ObjectId,
ref: 'Budget'
},
category: {
type: Schema.Types.ObjectId,
ref: 'Category'
}
}https://github.com/airbnb/javascript#whitespace--comma-spacing
https://github.com/airbnb/javascript#whitespace--in-braces
| const Router = require('express').Router() | ||
| const Expenses = require('./expenses') | ||
|
|
||
| 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.message))
})
.catch(err => res.status(500).json(err.message))
})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! You are doing a great job, John! 😊 Please review the individual comments I left then close this PR once done. |
No description provided.