【问题标题】:Meteor Routes & Sessions -> How to coordinateMeteor Routes & Sessions -> 如何协调
【发布时间】:2016-02-12 00:28:08
【问题描述】:

Meteor 问题:实现真实会话的最佳方式是什么? 我有一个带有统计信息的正常登录页面。没有问题。

现在,我希望人们能够使用特定的 URL 进行签到;假设他们以后 点击http://localhost:3000/checkin/area1之类的网址

如何与他们的登录协调?

我有签到路线:

Router.route('/checkin/:_id', function () {
  var req = this.request;
  var res = this.response;

  this.userId = 'steve';  //TODO: Need way to pull userId
  if (!this.userId) {
    res.end('Please login first');
  } else {
    //Verify correct area
    //Verify that haven't check before
    var lastCheckin = checkins.find({ user: this.userId, visited: this.params._id });
    if (lastCheckin.count() == 0) {
        //we haven't checkedin yet
        checkins.insert({ user: this.userId, visited: this.params._id, createdAt: new Date() })
        res.end('Checkin '+this.userId+' for '+this.params._id);
    } else {
        console.log('already checked in '+lastCheckin.createdAt);
        res.end(this.userId+' already visited '+this.params._id);
    }
  }
}, {where: 'server'});

我尝试过的事情:

  • 持久会话:但这不起作用,因为请求不是来自主页面,因此没有要提取的会话变量。
  • 在请求中拉取 cookie(因为持久会话似乎有 1 个)。原始登录似乎没有请求对象,所以我不知道从哪里得到它。
  • 其他(差异情况)在路由中显示了 Meteor.user(),但软件抱怨它只能在方法中使用。路由中可以使用什么?

我还能尝试什么?

感谢您的帮助。

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    好的,我不确定这是否会有所帮助。

    如果您在 /myPage 上,那么您有一个正在运行的事件:

    Router.go('/yourPage');
    Session.set('yourVariable', "yourValue");
    

    您将能够访问 /yourPage(和 yourPage 的代码)中的会话变量 (yourVariable)。

    【讨论】:

    • Session.set 去哪儿了?我目前在他们登录时已经设置好了。但是对于 /yourPage 示例,它何时包含在请求中?如果我查看从 /checkin 返回的页面,则没有 javascript 或任何内容,因此客户端没有任何内容检查任何会话。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 2013-10-24
    • 1970-01-01
    • 2011-06-20
    相关资源
    最近更新 更多