刷新页面与流星铁路由器

时间:2015-05-03 16:55:59

标签: meteor iron-router

问题在于:

我目前正根据我在github(https://github.com/sasikanth513/chatDemo)上找到的内容编写chatapp 我用铁路由器重构它。

当我进入页面(点击链接)时,我会得到一个现有的聊天室(这就是我想要的) 当我刷新页面(F5)时,我得到一个新创建的聊天室! (我想要的是获得现有的聊天室...)

以下是ironrouter中的代码:

Router.route('/chatroom', {
  name: 'chatroom',
  data: function() {

    var currentId = Session.get('currentId'); //id of the other person
    var res=ChatRooms.findOne({chatIds:{$all:[currentId,Meteor.userId()]}});
    console.log(res);
    if(res){
      Session.set("roomid",res._id);
    }
    else{
      var newRoom= ChatRooms.insert({chatIds:[currentId, Meteor.userId()],messages:[]});
      Session.set('roomid',newRoom);
    }

  }
});

您可以在整个项目中找到我的github仓库:https://github.com/balibou/textr

Thanx很多!

1 个答案:

答案 0 :(得分:1)

在meteor中,Session对象在客户端启动时为空,并通过HTTP加载/刷新页面"重新启动"客户端。要解决此问题,您可以在Meteor.user属性中保留用户的通讯ID,以便您可以轻松完成:

Router.route('/chatroom', {
  name: 'chatroom',
  data: function() {
    var currentId = Meteor.user().profile.correspondentId;
    var res=ChatRooms.findOne({chatIds:{$all:[currentId,Meteor.userId()]}});
    console.log(res);
    if(res){
      Session.set("roomid",res._id);
    }
    else{
      var newRoom= ChatRooms.insert({chatIds:[currentId, Meteor.userId()],messages:[]});
      Session.set('roomid',newRoom);
    }
  }
});

这可以使用适当的权限,但我建议不允许在客户端上直接更新该值(我不知道您是否希望用户能够覆盖其CorrespondentId)。因此,如果您想要保护此过程,请使用服务器方法调用替换所有代码,以便更新更安全。

另一个(也是更常见的情况)解决方案由David Weldon提供,如果您不介意在您的网址中包含ID(因此不是单个网址)

相关问题