【问题标题】:Meteor session keeps resetting in cartMeteor 会话在购物车中不断重置
【发布时间】:2015-04-15 04:17:03
【问题描述】:

我正在使用 Meteor 建立一个网上商店。我遇到了一个问题,即当将商品添加到购物车时,Meteor 会话会被重置。用户可以从两个位置将商品添加到购物车 - 主产品列表页面和产品详细信息页面。

场景 A:客户在主产品列表页面上并将商品添加到购物车。然后,客户单击另一个项目的产品详细信息页面。然后,客户从产品详细信息页面将商品添加到购物车。创建了一个新会话,添加到产品列表页面上的项目消失了,只有产品详细信息页面中的产品在购物车中。

所以我的问题是,将商品添加到购物车时会创建一个新会话,我不确定为什么会发生这种情况......

这是来自我的 productDetails.js 的 addToCart 点击事件:

'click .add-to-cart': function (e, tmpl) {
e.preventDefault();

var quantity = $('[name=qty]').val();
var thisProduct = Products.findOne();
var sessionId = Meteor.default_connection._lastSessionId;
var productInfo = {
  productCode: thisProduct.productCode,
  memPrice: thisProduct.memPrice,
  brand: thisProduct.brand,
  size: thisProduct.size,
  description: thisProduct.description,
  quantity: quantity,
  sessionId: sessionId
};
Session.set('sessionId', sessionId);

console.log(productInfo);

if (quantity > 0) {
  Meteor.call('addToCart', quantity, productInfo);
  Router.go('Tires');
} else {
  alert('Please input a desired quantity');
}

}

这是我的主产品页面中的 addToCart 点击事件:

'click .add-to-cart': function (e, tmpl) {
e.preventDefault();

var currUser = Meteor.user();   
if(!currUser) {
  alert("Please register for an account before you may add items to cart");
} else if (!currUser.profile.confirmed) {
  alert("Your account needs to be confirmed before you may add items to cart. Please contact info@info.org for assistance.")
} else {
  var currentRow = $(e.target).closest('tr');
  var quantity = currentRow.find('.item-quantity').val();
  var sessionId = Meteor.default_connection._lastSessionId;
  var productInfo = {
    productCode: this.productCode,
    memPrice: this.memberPrice,
    brand: this.brand,
    size: this.size,
    description: this.description,
    quantity: quantity,
    sessionId: sessionId
  };
  Session.set('sessionId', sessionId);

  if (quantity > 0) {
    Meteor.call('addToCart', quantity, productInfo);
    currentRow.find('.item-quantity').val(0);
  } else {
    alert('Please input a desired quantity');
  }
};

}

这是我的 addToCart 方法:

addToCart: function(qty, productInfo, cb) {
    console.log('//-------------Item Data-------------');
    console.log("Product Info: ", productInfo);

    if (qty > 0) {
        Cart.insert(productInfo);
    }
}

有什么想法吗?提前谢谢!

【问题讨论】:

    标签: session meteor


    【解决方案1】:

    Meteor.default_connection._lastSessionId 为您提供 websocket“会话”ID,它不是持久的,也不是像 cookie 一样跟踪用户会话的方法。 您应该依赖用户帐户并将购物车存储在配置文件中,或者使用另一个 Mongo 集合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 2011-08-18
      相关资源
      最近更新 更多