简体   繁体   English

重新加载页面时,流星上的会话值重置

[英]Session values on Meteor reset when page reloads

The values I define don't persist when page reloads. 重新定义页面时,我定义的值不会持久。

if (Meteor.isClient) {
    Session.setDefault('user_id', null);

    Template.register.events({
        'submit form': function(event){
            var password = getHash(event.target.password.value);
            var name = event.target.name.value;

            console.log(Session.get('user_id')); //always show the default

            var now = new Date();
            password = getHash(password);

            IDS.insert({name: name, online: false, password: password, last_activity: now,last_login: now, created_at: now},function(error,id){
                Session.set('user_id',id);
                //not persisting
            });

            event.target.password.value = '';
            event.target.name.value = '';
        }
    });
}

What am I doing wrong? 我究竟做错了什么?

As Marius Darila explained in the comments, the meteor Session does not persist accross browser refreshes. 正如马里乌斯·达里拉(Marius Darila)在评论中解释的那样,流星会话在浏览器刷新过程中不会持续存在。 It only persists within the client app. 它仅在客户端应用程序内保留。 When you refresh the page on the browser, it essentially "reboots" the client, resetting its Session to an empty state. 在浏览器上刷新页面时,它实际上是“重新引导”客户端,将其Session重置为空状态。 (plus whatever variables you set by default at startup, such as your user_id set at null ) (加上在启动时默认设置的任何变量,例如user_id设置为null

By default in Meteor, data can only be persisted across app reboots in the mongodb database. 在Meteor中,默认情况下,数据只能在重新启动应用程序后在mongodb数据库中保留。 You can find many community packages that give you other means of data persistency, such as the persistent session package for browser localStorage, or CollectionFS for server-side file storage. 您可以找到许多社区软件包,这些软件包为您提供了其他数据持久性方式,例如用于浏览器localStorage的持久性会话软件包,或用于服务器端文件存储的CollectionFS

Session doesn't persist when you manually refresh a page. 当您手动刷新页面时,会话不会持续。 All previous answer from pingo and BraveKenny are right on that point. pingo和BraveKenny的所有先前答案都正确地指向了这一点。 But when Meteor does a Hot Code Reload, it will persist Session data because the refresh has not been asked by user but by the server. 但是当Meteor执行热代码重载时,它将保留会话数据,因为刷新不是由用户而是由服务器要求的。 This is the only usecase where Session are persisted on a refresh, but it should not happen very often. 这是Session保持刷新的唯一用例,但这种情况不应该经常发生。

Need to add this line: 需要添加这一行:

Tasks = new Mongo.Collection("users");

to the server section: 到服务器部分:

if (Meteor.isServer) {
 // This code only runs on the server
   Tasks = new Mongo.Collection("users");
}

This will persist the data. 这将保留数据。 Alternatively, you can add it above the if (Meteor.isClient) section of the js file. 或者,您可以将其添加到js文件的if(Meteor.isClient)部分上方。

https://www.meteor.com/tutorials/blaze/collections https://www.meteor.com/tutorials/blaze/collections

New at this but happen to be learning Meteor as well. 这是新手,但碰巧也在学习流星。 Let me know if this works. 让我知道这个是否奏效。 Thanks ! 谢谢 !

IMHO, you are not doing wrong, just in an incorrect way! 恕我直言,你没有做错,只是以一种不正确的方式! Why don't you use account package and use Meteor.userId() instead? 为什么不使用帐户包,而是使用Meteor.userId()呢? And it is reactive too! 而且它也是反应性的! Btw session means client local storage, not on server side and its empty every time browser reloads. Btw会话表示客户端本地存储,不在服务器端,并且每次重新加载浏览器时都为空。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM