基于Express实现加入购物车功能

这是一个系列,所以有的东西不在这一篇里面,得去看我写的其他文章

要加入购物车首先要登录,假设用户已经登录成功
建立一个用户模型
models/users.js
var mongoose = require('mongoose')

var userSchema = new mongoose.Schema({
    'userId': Number,
    'userName': String,
    'userPwd': String,
    'orderList': Array,
    'cartList': [
    {
        'productId': Number,
        'productName': String,
        'salePrice': Number,
        'productImage': String,
        'checked': String,
        'productNum': Number
    }
    ],
    'addressList': [
    {
        'addressId': String,
        'userName': String,
        'streetName': String,
        'postCode': Number,
        'tel': Number,
        'isDefault': Boolean
    }
    ]
})

module.exports = mongoose.model('User', userSchema, 'users')
route/goods.js
// 加入购物车
router.post('/addCart', function(req, res, next) {
	// 假设用户已经登录
    var userId = 1809104005
    // 获取前端传递过来的商品ID
    var productId = req.body.productId
    // 加载用户模型
    var User = require('../models/users')
    // 找到该用户
    User.findOne({ userId: userId }, function (err, userDoc) {
        if (err) {
            res.json({
                status: 1,
                msg: err.message
            })
        } else {
            if (userDoc) {
                let goodsItem = ''
                // 如果用户要购买这个商品两次,那么只是该商品数量加上相对应的数量
                userDoc.cartList.forEach((item) => {
                    if (item.productId === productId) {
                        goodsItem = item
                        item.productNum++
                    }
                })
                if (goodsItem) {
                // 向数据库中保存相对应的数据
                    userDoc.save(function (err2, doc2) {
                        if (err2) {
                            res.json({
                                status: 1,
                                msg: err2.message
                            })
                        } else {
                            res.json({
                                status: 0,
                                msg: '',
                                result: 'suc'
                            })
                        }
                    })
                } else {
                // 根据商品的ID找到相对应的商品
                    Gooods.findOne( {productId: productId}, function (err1, doc) {
                        if (err1) {
                            res.json({
                                status: 1,
                                msg: err1.message
                            })
                        } else {
                            if (doc) {
                                doc.productNum = 1
                                doc.checked = 1
                                userDoc.cartList.push(doc)
                                userDoc.save(function (err2, doc) {
                                    if (err2) {
                                        res.json({
                                            status: 1,
                                            msg: err2.message
                                        })
                                    } else {
                                        res.json({
                                            status: 0,
                                            msg: '',
                                            result: 'suc'
                                        })
                                    }
                                })
                            }
                        }
                    })
                }
            }
        }
    })
})
调用该函数
addCart (productId) {
      axios.post('/goods/addCart', {
        productId: productId
      }).then((response) => {
        console.log(response)
        if (response.data.status === 0) {
          alert('加入购物车')
        }
     })
  },

你可能感兴趣的:(Node)