【问题标题】:an e-commerce project with MEANMEAN的电子商务项目
【发布时间】:2016-05-21 00:00:38
【问题描述】:

我正在做一个电子商务项目,当客户选择要购买的产品时,我被堆积在购物车中,当他验证产品时,我无法更新总数量的新值购买的产品。

我正在使用 MEAN 技术,对于猫鼬模型,我有一个用于 Poduct 产品型号

`var productModel = new Schema({
  title : {type:String,required : true},
  imgUrl : {type:String,required : true},
  price  :{type:Number,required : true},
  quantity : {type:Number,required : true},
  ref : {type:String,required : true},
  category : {type:String,required : true},
  descr : {type:String},
});`

and a **user Model** : 
var User = new Schema({
  firstname : {type : String },
  lastname : {type : String },
  username : {type : String, unique : true },
  role : { type:String , default : "customer"},
  password : {type : String },
  cart : [{
    productId : String,
    title : String,
    imgUrl : String,
    price : Number,
    quantity : Number,
    qte : Number,
    totalPrice : Number
  }],
  purchasedProds : [{
    productId : String,
    title : String,
    imgUrl : String,
    price : Number,
    qte : Number,
    totalPrice : Number,
    date : {type : Date, default : Date.now}
  }]

});

在服务器端,我使用了两个路由,一个 productRouter 用于处理产品 CRUD,一个 userRouter 用于处理用户身份验证及其购物车。 当用户验证其购物车时,我将清除用户集合中的购物车字段并将其插入到 purchasedProds 字段中,我成功地做到了这一点,就像我上面提到的那样,我无法更新新值购买产品的总量。 我已经尝试了超过 3 天,但它不起作用...请任何帮助将不胜感激...谢谢

【问题讨论】:

    标签: angularjs node.js mongodb express mongoose


    【解决方案1】:

    totalPrice 不应该是数组的元素。它应该是您的购物车对象的一个​​属性。像这样的东西;

    cart: [totalPrice : Number, items : {
        productId : String,
        title : String,
        imgUrl : String,
        price : Number,
        quantity : Number,
        qte : Number        
    }]
    

    因此,每次将 item 添加到您的 cart 时,您都应该将其添加为购物车的商品并自动添加 price将此商品添加到购物车的 totalPrice 中。

    【讨论】:

    • 我的意思是这里的totalPrice 是一种产品的总价,假设一个客户购买了一种产品的3 件,所以这里的totalPrice 是3 * prodPrice,但我的问题是如何更新总数量减少后的产品
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多