Meteor Collections that expire

前端 未结 2 1305
无人及你
无人及你 2021-01-21 10:12

I would like to set a session bound value that\'s synced between client and server in Meteor. I\'m thinking that should be done in a Collection, since Session isn\'t synced betw

2条回答
  •  一向
    一向 (楼主)
    2021-01-21 11:04

    You could remove the records for a collection after a certain amount of time.

    Server side code:

    //Run every minute
    Meteor.setInterval(function() {
    
        MyCollection.remove({expires: { $gte: new Date() }});
    
    }, 60000);
    

    When you insert it you can set an expiry:

    Server side code:

    MyCollection.deny({
        insert: function(userId, doc) {
            //Set expiry to 24 hours from now
            doc.expires = new Date( new Date().getTime() + (36000000*24) );
    
        }
    });
    

    The deny method adds the expiry time on the server, even if you add a document from the client. This way you can ensure documents expire regardless of the time on the client, which can often be incorrect.

    Then you can insert the document on the client side

    Client side

    MyCollection.insert({ base64data: xxxxxx });
    

    Also yes you're right, Session does not sync with the server.

提交回复
热议问题