michael wagner express mongo eval#157
michael wagner express mongo eval#157Michael-Wagner459 wants to merge 3 commits intoprojectshft:masterfrom
Conversation
| mongoose.connect('mongodb://localhost/products', { | ||
| useNewUrlParser: true, | ||
| useUnifiedTopology: true, | ||
| }); |
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:
mongoose.connection.on("connected", () => {
console.log("Connected to MongoDB successfully");
});
mongoose.connection.on("error", (err) => {
console.error(`Failed to connect to MongoDB: ${err.message}`);
});
| const Product = require('../models/product'); | ||
| const Review = require('../models/review'); | ||
|
|
||
| router.get('/generate-fake-data', (req, res, next) => { |
There was a problem hiding this comment.
check your code lint settings and alignment. Too much padding per line.
| product: '66917821a673574ec36a3a1d', | ||
| }); | ||
|
|
||
| // review.save(); |
| try { | ||
| const totalCount = await Product.countDocuments(query); | ||
| if (totalCount === 0) { | ||
| return res.status(404).send('No products found'); |
There was a problem hiding this comment.
is it really and error if no products were found?
404 is not the right error as it refers to a resource in the url not found, if you make a request for: '/prodcts' i would expect to see 404
| }) | ||
| .catch((err) => { | ||
| if (err.name === 'CastError') { | ||
| return res.status(404).send('Product not found'); |
There was a problem hiding this comment.
same, the request is successful, but the product was not found
| const { category, name, price, image } = req.body; | ||
|
|
||
| if (!category || !name || !price || !image) { | ||
| return res.status(400).send('All fields are required'); |
|
|
||
| res.status(200).send(savedProduct); | ||
| } catch (err) { | ||
| res.status(500).send('Server Error'); |
There was a problem hiding this comment.
in general we want to do something with the server error, if you are not sending it to the client at least log it into the console.
| if (deletedReview) { | ||
| return res.status(404).send('Review not found'); |
There was a problem hiding this comment.
this is a bug, if you find a deleted review, you are sending an error instead of deleting it
| if (deletedReview) { | |
| return res.status(404).send('Review not found'); | |
| if (!deletedReview) { | |
| return res.status(404).send('Review not found'); |
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import { fetchProducts, setPage } from '../slices/productsSlice'; | ||
|
|
||
| const Pagination = () => { |
There was a problem hiding this comment.
what is this component? I don't think its in use
No description provided.