当我刷新页面时,Meteor.userId不会持久

我正在使用Meteor 0.6.3.1,并且自己制作了用户系统(不是真正的用户系统,但是我想我也可以使用userIdvariables,因为没有其他人声明)。

问题是,variables不是持久的。

我有这个代码

Meteor.methods({ 'initCart': function () { console.log(this.userId); if(!this.userId) { var id = Carts.insert({products: []}); this.setUserId(id); console.log("cart id " + id + " assigned"); } return this.userId; } }); 

问题是,你应该能够切换页面,但仍然使用相同的购物车。

我不能使用会话,因为它们是客户端,并可能导致用户之间的信息泄漏..

我应该怎么做呢? 有没有像服务器端meteor放大?

来自meteor文件:

setUserId不具有追溯性。 它会影响当前的方法调用以及将来的方法调用。 之前任何调用此连接的方法仍会看到在启动时有效的userId的值。

当你刷新你创build一个新的连接 。 在这个连接上,你使用用户系统存储在客户端的cookielogin。

您可以将购物车ID存储在Cookie中

这适用于我:

 # server/methods/app.coffee #--------------------------------------------------- # Info found in Meteor documentation (24 Feb. 2015) # # > Currently when a client reconnects to the server # (such as after temporarily losing its Internet connection), # it will get a new connection each time. The onConnection callbacks will be called again, # and the new connection will have a new connection id. # > In the future, when client reconnection is fully implemented, # reconnecting from the client will reconnect to the same connection on the server: # the onConnection callback won't be called for that connection again, # and the connection will still have the same connection id. # # In order to avoid removing data from persistent collections (ex. cartitems) associated with client sessionId (conn.id), at the moment we'll implement the next logic: # # 1. Add the client IP (conn.clientAddress) to the cartitems document (also keep the conn.id) # 2. When a new connection is created, we find the cartitems associated with this conn.clientAddress and we'll update (multi: true) the conn.id on the all cartitems. # 3. Only remove the cartitems associated with the conn.id after 10 seconds (if in this time the customer reconnect, the conn.id will have been updated at the point #2. we avoid this way removing after refresh the page for example. # 4. After 10 seconds (ex. the user close the window) we'll remove the cartitems associated with the conn.id that it has been closed. Meteor.onConnection (conn) -> CartItems.update({clientAddress: conn.clientAddress}, {$set: {sessionId: conn.id}}, {multi: true}) conn.onClose -> Meteor.setTimeout -> CartItems.remove {sessionId: conn.id} , 10000 Meteor.methods getSessionData: -> conn = this.connection {sessionId: conn.id, clientAddress: conn.clientAddress}