我一直得到一个没有定义user_key的引用错误

时间:2016-03-15 16:51:54

标签: javascript meteor

  if(Meteor.isClient)
    {
        userKey = localStorage.getItem("user_key");
        if(!userKey){
            userKey = Meteor.uuid();
            localStorage.setItem("user_key", userKey);
        }

        getCart = function(next){
            Meteor.call("getCart", next);
        };

        addToCart = function (id, callback) {
            Meteor.call('addToCart',userKey, id, callback);
        };
        removeFromCart = function (id, callback) {
            Meteor.call('removeFromCart',userKey, id, callback);
        };
        updateCart = function (id, quantity, callback) {
            Meteor.call('updateCart', userKey, id, quantity, callback);
        };
    }

    if(Meteor.isServer) {
        Meteor.methods({

            "getCart": function (userKey) {
                check(userKey, String);
                return Carts.getCart(userKey);
            },
            "addToCart": function (userKey, id) {
                check(userKey, String);
                check(id, String);
                var cart = Meteor.call("getCart", userKey);

                //get the item in the cart
                var found = _.find(cart.items, function (item) {
                    return item._id === id;
                });

                if (found)
                {
                    found.quantity++;
                }
                else
                {
                    //add the item
                    var product = Products.findOne({_id: id});
                    var item = {
                        id: product._id,
                        name: product.productName,
                        price: product.price,
                        shop: product.shop,
                        image: product.productImg,
                        discount: 0,
                        added_at: new Date(),
                        quantity: 1
                    };
                    cart.items.push(item);

                }
                cart.notes.push({
                    note: id + " added to cart",
                    created_at: new Date()
                });
                //save it
                Meteor.call("saveCart", cart);
                return cart;
            },
            "updateCart": function (userKey, id, quantity)
            {
                check(userKey, String);
                check(id, String);
                check(quantity, Match.Where(function (quantity)
                {
                    check(quantity, Number);
                    return quantity >= 0;
                }));
                var cart = Meteor.call("getCart", userKey);
                //only update the quantity here
                _.each(cart.items, function (item)
                {
                    if (item._id === id)
                    {
                        item.quantity = quantity;
                        return Meteor.call("saveCart", cart);
                    }
                });
            },
            "removeFromCart": function (userKey, id)
            {
                check(userKey, String);
                check(id, String);
                var cart = Meteor.call("getCart", userKey);
                //get the item in the cart

                var found = _.find(cart.items, function (item)
                {
                    return item._id === id;
                });

                if (found)
                {
                    var foundIndex = cart.items.indexOf(found);
                    cart.items.splice(foundIndex, 1);
                    cart.notes.push({
                        note: id + " removed from cart",
                        created_at: new Date()
                    });
                    Meteor.call("saveCart", cart);
                }

                return cart;
            },
            "saveCart": function (cart) {
                check(cart, Match.ObjectIncluding({
                    userKey: String,
                    items: [Match.ObjectIncluding({
                        id: String
                    })]
                }));

                var products = Products.find({
                    _id: {$in: _.pluck(cart.items, '_id')}
                }).fetch();

                var skuMap = _.object(_.pluck(products, '_id'), products);

                cart.updated_at = new Date();
                cart.total = 0;
                var counter = 0;
                _.each(cart.items, function (item)
                {
                    item.price = skuMap[item._id].price;
                    item.quantity = Math.max(0, item.quantity);
                    // TODO: Don't trust discount from client
                    item.lineTotal = (item.price - item.discount) * item.quantity;
                    cart.total += item.lineTotal;
                    counter++;
                });
                cart.itemCount = counter;
                Carts.update({userKey: cart.userKey}, cart, {upsert: true});
                return cart;
            },
            "emptyCart": function (userKey)
            {
                check(userKey, String);
                Carts.remove({userKey: userKey});
            }
        })
    }

上面的代码适用于简单的购物车系统。获取用户uuid,将其设置为user_key并正常添加到购物车,获取购物车和更新购物车操作。我已经检查了我的浏览器,我知道userkey正在localStorage中设置,但是当我调用addToCart方法时,我在下面的图片中不断收到以下错误。

enter image description here
这是我下面的Carts.getCart函数。

Carts.getCart = function()
{
    var cart = Carts.findOne({userKey : userKey});

    if(!cart){
        cart = {
            userKey : userKey,
            email : null,
            name :null,
            ip : null,
            created_at : new Date(),
            items : [],
            notes : [{
                note : "Cart created",
                created_at : new Date()
            }],
            status : "open",
            itemCount : 0,
            total : 0
        };
    }
    return cart;
};

2 个答案:

答案 0 :(得分:0)

似乎你需要在userKey之前添加var:

var userKey = localStorage.getItem("user_key");

还尝试保存会话并获取值:

localStorage.setItem("user_key", userKey);
Session.set("user_key", localStorage.getItem(userKey));

答案 1 :(得分:0)

我发现了问题,我忘了将userkey传递给getCart函数