The code snippet below aims to change a document field after 20 seconds and update the another document field separately within a server method.
// Reset the current timeout
Meteor.clearTimeout(this.timeoutHandle);
console.log('clearing interval from server');
// Restart the timeout with a new 10 seconds delay
var timeoutHandle = Meteor.setTimeout(function () {
Auctions.update({_id:this._id }, {
$set: { isOpen: false }
});
}, 10000);
Auctions.update({_id: this._id}, {
$set: {
timeoutHandle: timeoutHandle
}
});
}
This snippet gets called in the client. The log prints fine and the setTimeout also executes after 10 seconds. but the last updade function doesn’t work. it’s when the console invokes this error exception while invoking method 'doSomething' undefined
doSomething is the name of my server method. The clientside console out the following error message 500 internal server error.
actually when I inserted a document in database, I saved the handler in the timeoutHandle, thus i can get the handler from db. I hope I made my point clear
On server, a timeout handle is an object that cannot be JSONified, so it cannot be saved in a collection (see here, actually this is due to node.js, not Meteor).
However, are you sure you need this? A timeout handle is a session specific piece of data, so storing it in a collection (which is a persistent storage) might not be a good fit.
If you really need to do this, you can create your own handler system:
var myTimeoutHandle = Random.id();
myTimoutHandleMap[myTimeoutHandle] = timeoutHandle