在Meteor JS中使用会话问题?

时间:2015-02-09 06:25:00

标签: meteor

我需要了解Sessions.Actually我们使用默认会话,如session.set(key,value)和session.get(key)。在此默认会话中清除某些情况,如刷新等。

首先我使用meteor add u2622:persistent-session Pkg。使用此pkg会出现一个错误,即“未捕获的错误:Meteor目前不支持ObjectID以外的对象作为ID ”。

要克服这些问题,请使用amplify Sessions。但是有一个样本使用放大Sessions如下所示代码:

Js代码:

Messages = new Meteor.Collection("messages");
if (Meteor.isClient) {


  var AmplifiedSession = _.extend({}, Session, {
    keys: _.object(_.map(amplify.store(), function (value, key) {
      return [key, JSON.stringify(value)];
    })),
    set: function (key, value) {
      Session.set.apply(this, arguments);
      amplify.store(key, value);
    }
  });


  // counter starts at 0
  Session.setDefault('counter', 0);
  AmplifiedSession.set('no', 1);




  Template.hello.helpers({
    counter: function () {
      return Session.get('counter');
    }
  });

  Template.hello.helpers({
    no: function () {
      return AmplifiedSession.get('no');
    }
  });

  Template.hello.events({
    'click button': function () {
      // increment the counter when button is clicked
      console.log("Btn Clicked");
      Session.set('counter', Session.get('counter') + 1);
      AmplifiedSession.set('no',AmplifiedSession.get('no') + 1);
    }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup


  });
}

即使不工作。放大会话也在刷新时清除。我对此没有任何了解。所以请建议我为此做些什么。

先谢谢。

2 个答案:

答案 0 :(得分:0)

Try this package on atmosphere如果有帮助请告诉我。

meteor add u2622:persistent-session

答案 1 :(得分:-1)

在此特定示例中,在每次加载页面时,您正在运行AmplifiedSession.set('no', 1);,因此将“否”设置为1.这就是为什么在页面刷新时,“否”被设置为1.删除此行,然后更改此行AmplifiedSession.set('no',AmplifiedSession.get('no') + 1);以设置“否”的值(如果它不存在)。