async function createCart(userId, pid, quantity) {
    // const totalPrice = price * quantity;
    const cartId = uuidv4();
    var newCart = new Cart({
        uid: cartId,
        items: [{
            pid: pid,
            quantity: quantity
        }]
        // totalPrice: totalPrice
    });

    // Add cart id to user document
    await User.findOne({id: userId})
        .then((user) => {
            if (!user) {
                console.log("User not found when creating cart.");
             }

            const query_param_userId = user._id;
            const user_cartId = newCart.uid;

            User.findByIdAndUpdate(query_param_userId, { "cartId": user_cartId },{ new: true })
                .then((result) => console.log("User document updated with cart id."))
                .catch((err) => console.log("User document failed to update cart id."))
        })
        .catch(err => console.log("Error in finding user during cart creation."));

    // Save cart document
    let setCart = await newCart.save()
        .then(doc => doc)
        .catch(err => {
            console.log("Error in saving cart");
            return null;
        });

    return setCart;
}
// Initial creation of a fresh cart with an item(s)
Initial creation of a fresh cart with an item(s)
Initial creation of a fresh cart with an item ( s )
async function createCart(userId, pid, quantity) {
    const cartId = uuidv4();
    var newCart = new Cart({
        uid: cartId,
        items: [{
            pid: pid,
            quantity: quantity
        }]
    });
    await User.findOne({id: userId})
        .then((user) => {
            if (!user) {
                console.log("User not found when creating cart.");
             }
            const query_param_userId = user._id;
            const user_cartId = newCart.uid;
            User.findByIdAndUpdate(query_param_userId, { "cartId": user_cartId },{ new: true })
                .then((result) => console.log("User document updated with cart id."))
                .catch((err) => console.log("User document failed to update cart id."))
        })
        .catch(err => console.log("Error in finding user during cart creation."));
    let setCart = await newCart.save()
        .then(doc => doc)
        .catch(err => {
            console.log("Error in saving cart");
            return null;
        });
    return setCart;
}
async function createCart ( userId , pid , quantity ) { const cartId = uuidv4 ( ) ; var newCart = new Cart ( { uid : cartId , items : [ { pid : pid , quantity : quantity } ] } ) ; await User . findOne ( { id : userId } ) . then ( ( user ) => { if ( ! user ) { console . log ( "User not found when creating cart." ) ; } const query_param_userId = user . _id ; const user_cartId = newCart . uid ; User . findByIdAndUpdate ( query_param_userId , { "cartId" : user_cartId } , { new : true } ) . then ( ( result ) => console . log ( "User document updated with cart id." ) ) . catch ( ( err ) => console . log ( "User document failed to update cart id." ) ) } ) . catch ( err => console . log ( "Error in finding user during cart creation." ) ) ; let setCart = await newCart . save ( ) . then ( doc => doc ) . catch ( err => { console . log ( "Error in saving cart" ) ; return null ; } ) ; return setCart ; }
Initial creation of a fresh cart with an item(s)
Initial creation of a fresh cart with an item ( s )
// const totalPrice = price * quantity; // totalPrice: totalPrice // Add cart id to user document // Save cart document