Javascript 页面重定向后Meteor会话未定义

Javascript 页面重定向后Meteor会话未定义,javascript,mongodb,session,meteor,Javascript,Mongodb,Session,Meteor,我正在做一个游戏,需要一个球员的游说团,但没有帐户。有点像游戏。我使用Meteor会话来了解哪个玩家加入了大厅,这样我就可以返回该特定玩家的正确数据。我有一个join.js组件,用户在其中输入大厅访问代码和用户名。此组件还将用户重定向到大厅。Join.js位于/Join路线,而大厅位于/lobbie路线。下面是join.js handleSubmit方法,它接受用户输入并将其放入players集合: handleSubmit(event) { event.preventDefau

我正在做一个游戏,需要一个球员的游说团,但没有帐户。有点像游戏。我使用Meteor会话来了解哪个玩家加入了大厅,这样我就可以返回该特定玩家的正确数据。我有一个join.js组件,用户在其中输入大厅访问代码和用户名。此组件还将用户重定向到大厅。Join.js位于/Join路线,而大厅位于/lobbie路线。下面是join.js handleSubmit方法,它接受用户输入并将其放入players集合:

    handleSubmit(event) {
    event.preventDefault();
    var party = Players.findOne({code: this.refs.code.value});
    if(typeof party !== 'undefined') {
        Meteor.call('players.insert', this.refs.code.value, this.refs.name.value);
        var playerId = Players.findOne({"name": this.refs.name.value})._id;
        Meteor.call('players.current', playerId);
        location.href = "/" + this.refs.code.value;
    } else {
        document.getElementById("error").innerHTML = 'Please enter a valid party code';
    }
我正在使用players.js集合中Meteor.methods中的会话获取当前用户

import { Mongo } from 'meteor/mongo';
import { Session } from 'meteor/session';

Meteor.methods({
    'players.insert': function(code, name) {
        console.log('adding player: ', name , code);
        Players.insert({code: code, name: name});
    },
    'players.updateAll': function(ids, characters, banners, countries, ancestors) {
        for (var i = 0; i < characters.length; i++){
            Players.update({_id: ids[i]}, {$set: {character: characters[i], banner: banners[i], country: countries[i], ancestor: ancestors[i]},});
        }
    },
    'players.current': function(playerId) {
            Session.set("currentPlayer", playerId);
            console.log(Session.get("currentPlayer"));
    },
    'players.getCurrent': function() {      
            return Session.get("currentPlayer");
    }
});

export const Players = new Mongo.Collection('players');
根据,Meteor方法是定义从客户端调用的服务器端行为的方式。它们实际上是在服务器上定义的

方法是Meteor客户端可以使用Meteor.call调用的远程函数

客户机上定义的Meteor方法只是作为存根

在客户机上调用方法定义与同名服务器方法关联的存根函数

根据您的代码,看起来您正在客户端完成所有工作。事实上,会话是Meteor客户端API的一部分(不能在服务器上使用)

会话在客户端上提供了一个全局对象,可用于存储任意一组键值对

因此,如果我是你,我会在某种类型的util文件中实现所有这些逻辑,然后将其导入到需要的模板中。您实际上也在做同样的事情,您只需要使用常规函数而不是Meteor方法

下面是一个示例util文件(请确保根据项目的文件结构更新Players导入)


当然,您需要修改上述代码以使用正确的模板名称和UTIL文件的位置。

我认为问题在于您正在使用

location.href = "/" + this.refs.code.value;
而不是使用

Router.go("/"+this.refs.code.value);

如果使用铁路由器。这样做就像刷新页面一样。这是我的

我正在使用React路由器。将
Router.go(“/”+this.refs.code.value)还在工作吗?我想是的。你可以试试。我没有和React路由器一起工作过。您还应该考虑“JordANWLIIS”建议在服务器上处理更多。我可能会选择不带密码包的accounts base包。这样,您就可以只使用Meteor.userId()来处理当前用户。util文件会替换我所有的Meteor方法,还是只替换那些涉及会话的方法?@nickkt确定使用会话的任何内容,然后是可以在客户端上运行的任何内容(因为Meteor方法是用于服务器端的)@nickkt基本上,任何受保护的代码都应该在meteor方法中定义(仅放在服务器代码中)。集合插入通常是受保护的代码,一旦删除默认的
不安全的
包,它甚至无法在客户端上工作(除非您相应地配置了允许/拒绝规则)
import { Template } from 'meteor/templating';
import { players } from './utils.js';

Template.template_name.events({
  'click .class': handleSubmit (event, instance) {
    event.preventDefault();
    var party = Players.findOne({code: this.refs.code.value});

    if (typeof party !== 'undefined') {
        var playerId = players.insert(this.refs.code.value, this.refs.name.value);
        players.setCurrent(playerId);
        location.href = "/" + this.refs.code.value;
    } else {
        document.getElementById("error").innerHTML = 'Please enter a valid party code';
    }
  },
});
location.href = "/" + this.refs.code.value;
Router.go("/"+this.refs.code.value);