Closed talmax1124 closed 3 years ago
You could use find({}).sort({date: -1})
in your controller if you're trying to get the latest data
@kiranojhanp Which controller?
@carlos product controller
On Fri, 4 Dec 2020 at 17:44, Carlos notifications@github.com wrote:
@kiranojhanp https://github.com/kiranojhanp Which controller?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/bradtraversy/proshop_mern/issues/79#issuecomment-738822293, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOQNFUXFYURM5DJUWCJXEHLSTDYU7ANCNFSM4UMXR6QA .
-- Opio Elias Hezron Makerere University, Faculty of Technology.
@carlos product controller On Fri, 4 Dec 2020 at 17:44, Carlos @.**> wrote: @kiranojhanp https://github.com/kiranojhanp Which controller? — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#79 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOQNFUXFYURM5DJUWCJXEHLSTDYU7ANCNFSM4UMXR6QA . -- Opio Elias Hezron Makerere University, Faculty of Technology.*
It is not changing anything for me when i add it
Below is my code
import asyncHandler from 'express-async-handler' import Product from '../models/productModel.js'
// @desc Fetch all products // @route GET /api/products // @access Public const getProducts = asyncHandler(async (req, res) => { const pageSize = 12 const page = Number(req.query.pageNumber) || 1
const keyword = req.query.keyword ? { name: { $regex: req.query.keyword, $options: 'i', }, } : {}
const count = await Product.countDocuments({ ...keyword }) const products = await Product.find({ ...keyword }) .limit(pageSize) .skip(pageSize * (page - 1)).find({}).sort({date: -1}); // .sort()
res.json({ products, page, pages: Math.ceil(count / pageSize) }) })
// @desc Fetch single product // @route GET /api/products/:id // @access Public const getProductById = asyncHandler(async (req, res) => { const product = await Product.findById(req.params.id)
if (product) { res.json(product) } else { res.status(404) throw new Error('Product not found') } })
// @desc Delete a product // @route DELETE /api/products/:id // @access Private/Admin const deleteProduct = asyncHandler(async (req, res) => { const product = await Product.findById(req.params.id)
if (product) { await product.remove() res.json({ message: 'Product removed' }) } else { res.status(404) throw new Error('Product not found') } })
// @desc Create a product // @route POST /api/products // @access Private/Admin const createProduct = asyncHandler(async (req, res) => { const product = new Product({ name: 'Product', price: 0, user: req.user._id, image: 'https://www.diagmacr.com/wp-content/uploads/2016/07/2-640x510.jpg', brand: 'Creative Duo', category: '', countInStock: 1, numReviews: 0, description: '', })
const createdProduct = await product.save() res.status(201).json(createdProduct) })
// @desc Update a product // @route PUT /api/products/:id // @access Private/Admin const updateProduct = asyncHandler(async (req, res) => { const { name, price, description, image, brand, category, countInStock, } = req.body
const product = await Product.findById(req.params.id)
if (product) { product.name = name product.price = price product.description = description product.image = image product.brand = brand product.category = category product.countInStock = countInStock
const updatedProduct = await product.save()
res.json(updatedProduct)
} else { res.status(404) throw new Error('Product not found') } })
// @desc Create new review // @route POST /api/products/:id/reviews // @access Private const createProductReview = asyncHandler(async (req, res) => { const { rating, comment } = req.body
const product = await Product.findById(req.params.id)
if (product) {
const review = {
name: req.user.name,
rating: Number(rating),
comment,
user: req.user._id,
}
product.reviews.push(review)
product.numReviews = product.reviews.length
product.rating =
product.reviews.reduce((acc, item) => item.rating + acc, 0) /
product.reviews.length
await product.save()
res.status(201).json({ message: 'Review added' })
} else { res.status(404) throw new Error('Product not found') } })
// @desc Get top rated products // @route GET /api/products/top // @access Public const getTopProducts = asyncHandler(async (req, res) => { const products = await Product.find({}).sort({ rating: -1 }).limit(8)
res.json(products) })
export { getProducts, getProductById, deleteProduct, createProduct, updateProduct, createProductReview, getTopProducts, }
@eliashezron and @kiranojhanp or @basir @bradtraversy . Any idea?
@basir Where do I place the code that you have provided from the Udemy Response.
Never-mind. Got it to work. How can I display a
@basir @bradtraversy @kiranojhanp @eliashezron
You can send a get request to the end point if you wanna display it in the frontend. Where do you want it to be displayed?
I want to implement that badge to appear on the card itself in homescreen and possibly inside of the productscreen. But only 1 one it. So like if I add a new product I want it to switch to that new product. and display there until I add another product. Can you help me implement it? @kiranojhanp
Never-mind. Got it to work. How can I display a New from 'react-bootstrap' in only the newest product?
@basir @bradtraversy @kiranojhanp @eliashezron
I did not get your mean. please ask it clearly on the Q/A
@basir I did write it in udemy. No one has responded yet. Can you look at the posts from me or do you want me to provide the details
Never-mind. Got it to work. How can I display a New from 'react-bootstrap' in only the newest product? @basir @bradtraversy @kiranojhanp @eliashezron
I did not get your mean. please ask it clearly on the Q/A
Do you want the links?
Here are all the links to my udemy questions that still are unanswered: https://www.udemy.com/course/mern-ecommerce/learn/#questions/13274424 - How do i deploy with netlify?
https://www.udemy.com/course/mern-ecommerce/learn/#questions/13329082 - How do I show a new badge from react-bootsrap?
Allow users to upload files and send it to google drive or somewhere on an online service like google drive or one drive. don't know how to use s3. - https://www.udemy.com/course/mern-ecommerce/learn/#questions/13335096
Coupon code - https://www.udemy.com/course/mern-ecommerce/learn/#questions/13335122
Stripe - https://www.udemy.com/course/mern-ecommerce/learn/#questions/13335130
Image Slider - https://www.udemy.com/course/mern-ecommerce/learn/#questions/13335154
@basir @bradtraversy @eliashezron
All questions have been answered.
please ask the on the udemy. here is for bugs in the repository.
@basir Please revisit the Udemy links. I have clarified the questions you have asked.
I have been looking online and mongo documentation and I can't find anywhere that tells me how to show data by date added. It's urgent so if anyone can help me it would be appreciated. I tried coding with all the docs but no good result. Please.
@basir @bradtraversy