简体   繁体   English

页面重定向后流星会话未定义

[英]Meteor session is undefined after page redirect

I am making a game that requires a lobby of players, but no accounts. 我正在制作一个需要玩家游说但没有帐户的游戏。 Kind of like the game, Spyfall . 有点像游戏Spyfall I am using Meteor Sessions to know which player joined the lobby so that I can return the proper data for that specific player. 我正在使用Meteor Sessions来了解哪个玩家加入了大厅,以便我可以返回该特定玩家的正确数据。 I have a join.js component where the user enters in the lobby access code and the user's name. 我有一个join.js组件,用户可以在其中输入大厅访问代码和用户名。 This component also redirects the user to the lobby. 该组件还将用户重定向到大厅。 Join.js is at the route, /join, and the lobbies are at the route, /:lobby. Join.js位于/ join路由,而大堂位于/:lobby路由。 Here is the join.js handleSubmit method which takes the user input and puts it in the players collection: 这是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';
    }

I am using Sessions in the Meteor.methods in the players.js collection to get the current user. 我正在使用player.js集合中Meteor.methods中的Sessions来获取当前用户。

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');

The console.log in the 'players.current' method returns the proper player id, but once the page redirects to /:lobby, the players.getCurrent returns undefined. 'players.current'方法中的console.log返回正确的玩家ID,但是一旦页面重定向到players.getCurrent将返回undefined。 I want players.getCurrent to return the same value that the console.log returns. 我希望players.getCurrent返回与console.log返回相同的值。 How do I fix this issue? 如何解决此问题? This is the function to get the current player id in the lobby.js: 这是在lobby.js中获取当前玩家ID的功能:

getCurrentPlayerId() {
    return Meteor.call('players.getCurrent');
}

Per the Meteor API , Meteor methods are meant to be the way you define server side behavior that you call from the client. 根据Meteor API ,Meteor方法是定义从客户端调用的服务器端行为的方式。 They are really intended to be defined on the server. 它们实际上是要在服务器上定义的。

Methods are remote functions that Meteor clients can invoke with Meteor.call. 方法是Meteor客户端可以通过Meteor.call调用的远程函数。

A Meteor method defined on the client simply acts as a stub. 在客户端上定义的Meteor方法只是充当存根。

Calling methods on the client defines stub functions associated with server methods of the same name 客户端上的调用方法定义与同名服务器方法相关联的存根函数

Based on your code it looks like you are doing everything client side. 根据您的代码,您似乎在做客户端的所有工作。 In fact, session is part of the Meteor client API (can't use on the server). 实际上,会话是Meteor客户端API的一部分(不能在服务器上使用)。

Session provides a global object on the client that you can use to store an arbitrary set of key-value pairs. 会话在客户端上提供了一个全局对象,可用于存储任意一组键值对。

Therefore, If I were you, I would just implement all this logic in some sort of util file that you can then import into the Templates where you need it. 因此,如果我是您,那么我只会在某种util文件中实现所有这些逻辑,然后可以将其导入到需要的模板中。 You are effectively doing the same thing, you just need to use regular functions instead of Meteor methods. 您实际上是在做同样的事情,只需要使用常规函数而不是Meteor方法即可。

Here is an example util file (be sure to update the Players import based upon your project's file structure). 这是一个示例util文件(请确保根据项目的文件结构更新Players导入)。

import { Players } from './players.js';
import { Session } from 'meteor/session';

export const players = {
  insert: function(code, name) {
    console.log('adding player: ', name , code);
    return Players.insert({code: code, name: name});
  },

  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]},});
    }
  },

  setCurrent: function(playerId) {
    Session.set("currentPlayer", playerId);
    console.log(Session.get("currentPlayer"));
  },

  getCurrent: function(unixTimestamp) {
    return Session.get("currentPlayer");
  },
};

Then, you can import this into whatever template you have that has defined the event handler you included in your question. 然后,可以将其导入到定义了问题中包含的事件处理程序的任何模板中。

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';
    }
  },
});

Of course you will need to modify the above code to use your correct template name and location of the utils file. 当然,您将需要修改以上代码以使用正确的模板名称和utils文件的位置。

I think the issue is that you are using 我认为问题是您正在使用

location.href = "/" + this.refs.code.value;

instead of using 而不是使用

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

if using Iron Router. 如果使用Iron Router。 Doing this is as if you are refreshing the page. 就像刷新页面一样。 And here's a package to maintain Session variables across page refreshes . 这是一个用于在整个页面刷新期间维护Session变量的程序包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 会话变量在Meteor中未定义 - Session variable is undefined in Meteor 重定向页面后会话值未更新 - Session value not update after redirect the page 销毁会话后重定向到上一页 - Redirect to previous page after destroying session 如何使用Meteor js在Stripe成功后重定向到成功页面 - How to redirect to a success page After Stripe success with Meteor js 在Meteor中重新加载页面后,为什么会收到未定义的值? - Why do I receive undefined value after reloading page in Meteor? 在其他页面上阅读流星会话 - Read Meteor Session on a different page 单击事件后如何更改会话值,以便页面刷新不会在Meteor上重置会话 - How to change a session value after a click event, so that a page refresh does not reset the session on Meteor 流星:在Router.route()中的Session.set在“客户端”页面中使用时未定义 - Meteor: Session.set, inside Router.route(), is then undefined when used in a 'client' page 会话超时或重新启动应用程序后重定向到登录页面 - Redirect to login page after session timeout or restarted application 会话过期后如何使整个页面重定向到登录在MVC中? - How to make entire page redirect to login after session expires in mvc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM