购物车不是构造函数

时间:2017-11-29 15:58:51

标签: javascript express

我正在学习express.js并尝试学习购物车/会话功能。 这是我的代码,我的问题将在底部/结尾。

我的cart.js

//console.log this function to make sure that the file is required correctly.
sayHello = function () {
    return "Hello in English";
};



var Cart = function Cart(oldCart) {
    this.items = oldCart.items;

    this.add = function (item, id) {
        var storedItem = this.items[id] = { item: item };
        storedItem.qty++
    };
    this.generateArray = function () {
        var arr = [];
        for (var id in this.items) {
            arr.push(this.items[id]);
        }
        return arr;
    };
};

module.exports;

我的路线有index.js,我在其中列出产品并点击任意产品将产品添加到我的购物车页面,所以这是我的index.js

 var express = require('express');
var router = express.Router();
var Cart = require('../lib/cart.js');
var db = require('../lib/db.js');
var database = require('../database.js')

var mongoose = require('mongoose');
var Products = mongoose.model('Product');
var User = mongoose.model('User');


mongoose.connect = ('mongodb://localhost/products');


/* GET home page. */
router.get('/', function (req, res) {
     console.log(sayHello());

    Products.find(function (err, products) {
        res.render('index', { title: 'Express 2017 ', products: products });
        console.log(products);
    });
});


router.post('/', function (req, res) {
   // var newUser = new User ({name : req.body.name});
   // newUser.save(function(err, users){
     //   res.render('index', {title: "whatever"})
    //});

     var newProduct = new Products({ name: req.body.name, description: req.body.description, price: req.body.price });
    newProduct.save(function (err, products) {
        res.render('index', { title: "PRODUCTS", products: products });
    });
});


router.get('/cart', function(req, res){

    res.render('cart');

});


//        <<<<<<<<This is where I need help with ....>>>>>>

router.post('/cart', (function (req, res) {
    //The function sayHello() prints Hello In English Correctly.
    console.log(sayHello());
    var productid = req.body.id

    //I get error saying "Cart" is not defined?? Why??
    var cart = new Cart(req.session.cart ? req.session.cart : { items: {}});

    //query the db and return the correct object
    Products.findById({ _id: productid }, function (err, product) {
        //to make sure the db is returning the right item
        console.log(product.name);
f
        //if there is an error...
        if (err) {
            return res.redirect('/');
        }

        //if all is well...then time to add items to the cart.
        //add the product to the cart object and add all this to the session.
        cart.add(product, product.id)
        req.session.cart = cart;

        //Let us see what is in the cart
        console.log(request.session.cart)
        //return the user to the products home page to add more items.
        res.redirect('/');
    });
}));


module.exports = router;

问题:一切都在主页上运行良好(将产品添加到数据库,显示它......等),当我点击&#34;添加到购物车&#34;按钮,我收到错误&#34; 购物车不是构造函数&#34; ...

我是js的新手,所以简洁,含糊的信息会飞过我的头脑而不会有所帮助,请慷慨解答你的答案,更多细节更好。

如果您有时间在代码中向我展示如何修复我的代码将非常感激

2 个答案:

答案 0 :(得分:2)

您永远不会在cart.js中导出任何内容,因此导入中的Cart是空白对象,而不是函数。

您可以替换它:

module.exports = Cart;

...或将其添加到导出并更改require来电:

module.exports.Cart = Cart;
// ...and in the other script...
var Cart = require('../lib/cart.js').Cart;
//                                  ^^^^^

要么是好的。如果Cart是唯一的出口,可能是前者而不是后者。

答案 1 :(得分:1)

您没有导入正在使用的Cart函数。

var cart = new Cart(req.session.cart ? req.session.cart : { items: {}});

您在模块module.exports = cart;导入

中遗漏了这个:myCart.js