Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

370
Views
Add to cart feature using Express, nodeJS and Mongoose

Im about to create the Add-to-cart feature to my project but I have some questions first. Do I need a Cart schema? Because I don't think I would store products to a cart Schema.

I started with this, creating a cart into user.

const UserSchema = new Schema({
    email: {
        type: String,
        required: true,
        unique: true
    },
    reputation: Number,
    cart: {
        items: [{
            productId: {
                type: mongoose.Types.ObjectId,
                ref: 'Product',
                required: true
            },
            qty: {
                type: Number,
                required: true
            }
        }],
        totalPrice: Number
    }
})

My User controller, of course empty.

module.exports.renderCart = (req, res) => {
    const product = req.body.product;
    // console.log(req.body.product)
    res.render('users/cart', { product })
}

It is okay to start with is? Also I want the cart session end at some point. Of course I need to create maybe a cart Class with methods like Add() remove() and so on. How can I do that and use it in the user controller?? Thank you in advance!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!