1

我正在使用 Meteor 0.6.3.1 并即兴创作了我自己的用户系统(不是真正的用户系统,但我认为我不妨使用 userId 变量,因为没有其他人声称它。)

问题是,变量没有持续存在。

我有这个代码

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 有类似 Amplify 的东西吗?

4

2 回答 2

2

来自 Meteor 文档:

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

当你刷新你创建一个新的连接。在该连接上,您使用客户端用户系统存储的 cookie 登录。

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

于 2013-07-17T21:35:04.000 回答
-1

这对我有用:

# 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}
于 2015-02-24T06:00:08.493 回答