Stephen Swaringin - Mongo/Express Eval#158
Stephen Swaringin - Mongo/Express Eval#158sswaringin wants to merge 36 commits intoprojectshft:masterfrom
Conversation
…or handling for invalid reviewId
… getProducts function
…ly pulling all products
…ng ProductCard component
… the ProductCategories component
…sort, or search field changes
| const products = require('./routes/products'); | ||
| const fakeData = require('./routes/fakeData'); | ||
|
|
||
| mongoose.connect('mongodb://localhost/products'); |
There was a problem hiding this comment.
next time add some connection helpers to see in the console when connections was established or not, example:
ongoose.connection.on("connected", () => {
console.log("Connected to MongoDB successfully");
});
mongoose.connection.on("error", (err) => {
console.error(`Failed to connect to MongoDB: ${err.message}`);
});
There was a problem hiding this comment.
Thanks for the feedback. It would have been nice for the Parsity material to include recommendations such as this if it is best practice.
There was a problem hiding this comment.
Stephen, your submission is excellent and this review and comments are just for your learning!
pinging Brian as indeed as well in case he thinks it should be added to the program
| } | ||
|
|
||
| async function getProducts(req, res, next) { | ||
| let page = Number(req.query.page); |
There was a problem hiding this comment.
| let page = Number(req.query.page); | |
| let page = Number(req.query.page) ?? 1 |
Should work the same as the check you are doing in line 38.
| const resultsLimited = results.slice(start, stop); | ||
|
|
||
| // Sends the paginated results to the client | ||
| res.send({ resultsCount, totalPages, page, resultsLimited, query, category }); |
There was a problem hiding this comment.
I think the division of responsibility is great, BUT!
The helper should not be sending the response back. A helper in general is a service that is actually helping you, having reusable functionality, etc. Here is way more than that, this service is reading the DB and even sending the responses back.
I would have either called this helper something else or separate even more the responsibilities. Specially leaving the response to be sent from the route.
| // A callback function to pull all categories | ||
| async function getCategories(req, res, next) { | ||
| // Query all products from MongoDB | ||
| const results = await Product.find({}); |
There was a problem hiding this comment.
you can get all categories without bringing all the products.
https://www.mongodb.com/docs/manual/tutorial/project-fields-from-query-results/#return-the-specified-fields-and-the-_id-field-only
| // A callback function to pull all categories | ||
| async function getCategories(req, res, next) { | ||
| // Query all products from MongoDB | ||
| const results = await Product.find({}); |
There was a problem hiding this comment.
be more specific, results of what?
| const results = await Product.find({}); | |
| const allProducts = await Product.find({}); |
| // A function to pull the reviews array from a product document and log review count | ||
| function getReviews(productObj) { | ||
| const reviews = productObj.reviews; | ||
| console.log({reviews: reviews.length}); |
There was a problem hiding this comment.
do not submit code with console logs
| } | ||
|
|
||
| // A function to remove the _id field from each review | ||
| function filterReviews(productObj) { |
There was a problem hiding this comment.
the function name is not really accurate, also why do you need it? seems not to be in use.
Clean your code clean!
| return res.status(401).send('Please provide a name, price, category, and image.'); | ||
| } | ||
|
|
||
| let product = new Product({name, price, category, image}); |
| const { name, price, category, image } = req.body; | ||
|
|
||
| if (!name || !price || !category || !image) { | ||
| return res.status(401).send('Please provide a name, price, category, and image.'); |
There was a problem hiding this comment.
wrong error, this should be 400. 401 and 403 are for authentication.
| const { username, text } = req.body; | ||
|
|
||
| if (!username || !text) { | ||
| return res.status(401).send('Missing required values: username, text'); |
No description provided.