Hey Tara! Great work on this project!! Here's my comments and feedback:
Double creating!
In your controller, you're creating the same thing twice!
|
async function create(req, res){ |
|
if (!req.user) { |
|
return res.redirect('/auth/google') |
|
} |
|
const newProduct = new Product(req.body); |
|
console.log('this is the NEW PRODUCT logged:',newProduct); |
|
console.log('This is REQ.BODY', req.body) |
|
await Product.create({ |
|
image: req.body.image, |
|
name: req.body.name, |
|
desc: req.body.desc, |
|
qty: req.body.qty, |
|
price: req.body.price, |
|
}) |
Remember that with Mongoose, we have two ways of creating things in our database:
- Creating a new object in memory with
let object = new Model()
, and then calling object.save()
on it to actually send it back to our database or
- Using the mongoose
Model.create()
method, which will actually do both of those steps in one.
The way your code is set up now, that newProduct
is absolutely useless! We're creating it in memory, but then not saving it. Make sure you give the lessons or The Docs if you need some more clarification.
Delete Functionality
You were so close with your delete function, and I think a little bit more console logging would have gotten you there!
|
function delCart(req,res,next){ |
|
console.log ('req.user',req.user) |
|
let cart= req.user.cart |
|
let idx = cart.findIndex(function (item){ |
|
console.log('this is item:'. item) |
|
item.equals(req.params.id) |
|
}) |
|
console.log('this is the index',idx) |
|
//i wasnt to write a splice method and save but the index # logging is always a -1 |
|
|
|
} |
Your cart
variable is going to be an array of objects, and therefore, the item
variable inside of your findIndex
callback is going to be an object. So if you want to compare the id of that object to the id that you have in your req.params
, then you'd have to change your code around so you're comparing item.id
with req.params.id
. That's why that method is constantly returning -1, because you're checking if an object is equal to a string (which obviously, will always fail)
Stay RESTful!
You got one route that isn't following RESTful conventions. Remember that when we're sending data with the intent of creating something in our database, we would want to send it as a POST request, or if we're updating something, it would be a PUT request. This is up to you in how you want to route this particular request, but it won't be a GET request!
|
router.get('/cart/:id',userCtrl.addToCart); |
Great work, and as always, let me know if you have questions about this feedback!