U
U
UNy2018-05-18 18:18:37
JavaScript
UNy, 2018-05-18 18:18:37

Shopping cart for an online store?

Made the user through sessions and further there is a question of realization of a basket. When you click the "Add to Cart" button next to a product, what should happen in order for the product to be added to the cart of this user? In the database for creating a user, the model is:

let mongoose = require('mongoose');

let User = new mongoose.Schema({
    name:{
        type:String,
        unique:true,
        required:true
    },
    password:{
        type:String,
        required:true
    }
});

exports.User = mongoose.model('User',User);

Should I add the Cart property as an array? And when you click, throw data into this property, and then take it and draw it in the Basket itself? Advise how to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Khokhlov, 2018-05-18
@UNy

let Cart = new mongoose.Schema({
    userId: { type: String, required: true, index: true, ref: 'User' },
    ...
});

I would make a separate collection. And the user's document is not overloaded with data, and it is more convenient to track abandoned carts, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question