As the title says I am a beginner learner who now got into learning the MERN stack. I am now currently using Node.js and Express for the server and about to create the client side in React.
My problems are I don’t know how to make all users’ orders appear for the admin, and how to make all the user’s checkouts appear to them but not the admin?
See the following photo on how a user’s orders reflect on the database (MongoDB). I test using Postman. POSTMAN PHOTO
See also my code on my routes and controllers on how a user checkouts an item.
CONTROLLERS
//USER CHECKOUT
module.exports.co = async (buy) => {
const {userId, productId} = buy
const buying = await User.findById(userId).then(result => {result.order.push({productId: productId})
return result.save().then(user => user ? true: false)
})
const bought = await Product.findById(productId).then(result => {
result.checkOut.push({userId: userId})
return result.save().then(product => product ? true : false)
})
if(buying && bought) {
return 'Item checked out.'
} else {
return 'Item not checked out.'
}
}
ROUTE
//User check-out
router.put('/:_id/check-out', verify, async (req, res) => {
const user = decode(req.headers.authorization).isAdmin
const buy = {
userId: decode(req.headers.authorization).id,
productId: req.body.productId
}
if(user == false){
try{
await co(buy).then(result => res.send(result))
}catch(err){
res.status(500).json(err)
}
}else {
res.send(`Only registered users can purchase items.`)
}
})