Tarang
Tarang

Reputation: 75945

Meteor performing a query involving time from the client

If I'm doing a

Collection.insert({"name":"Record 1",creationTime:new Date()});

from the client, because the command is going to be sent to the server anyway, is there a way to use the server's time instead of the client's time at the point of data insertion?

Using new Date(); may be inconsistent because the client's time could be anything.

I'm aware we could fetch the server's time before inserting the query, but it does seem a bit redundant considering the insertion command is going to be sent back to the server.

Upvotes: 1

Views: 92

Answers (1)

TimDog
TimDog

Reputation: 8928

You could simply do this to avoid the already-sending-to-server-redundancy:

if (Meteor.isClient) {
    Meteor.call("addItem", {"name": "Record 1"});
}

if (Meteor.isServer) {
    Meteor.methods({
        "addItem": function(obj) {
            obj.creationTime = new Date();
            Collection.insert(obj);
        }       
    });
}

Or from the client:

if (Meteor.isClient) {
    Meteor.call("getDate", function (error, result) {
        Collection.insert({"name":"Record 1", creationTime: result});
    });
}


if (Meteor.isServer) {
    Meteor.methods({
        getDate: function () {
            return new Date();
        }
    });
}

Upvotes: 1

Related Questions