How to Trigger Events in Node JS when a future date, time stored in Database is reached?
I am providing a functionality to the users where "They select a near future time, date while giving an Order. This Order time and date is stored in the database."
How can I trigger a function to perform something when the time and date in the database is reached?
I am going to use Node JS, Express JS, Mongo DB, React JS for the backend, server, database, and frontend respectively. I am asking for the architecture or creating such trigger of how this can be performed and not for complete code.
Any supporting NPM packages recommendation would be helpful. Thank you.
Note: This is my first application development in any language. I have learnt these technologies only from Udemy courses. The application will have Users select a date in the future and when the future date is reached, the application does some function for them.
javascript node.js reactjs mongodb triggers
add a comment |
I am providing a functionality to the users where "They select a near future time, date while giving an Order. This Order time and date is stored in the database."
How can I trigger a function to perform something when the time and date in the database is reached?
I am going to use Node JS, Express JS, Mongo DB, React JS for the backend, server, database, and frontend respectively. I am asking for the architecture or creating such trigger of how this can be performed and not for complete code.
Any supporting NPM packages recommendation would be helpful. Thank you.
Note: This is my first application development in any language. I have learnt these technologies only from Udemy courses. The application will have Users select a date in the future and when the future date is reached, the application does some function for them.
javascript node.js reactjs mongodb triggers
try using moment js
– the_ultimate_developer
Jan 2 at 7:01
usesetInterval
but limitation is that the page should not be reloaded. Or the other way you can create an interval after reloading page.
– Sameer Reza Khan
Jan 2 at 7:08
add a comment |
I am providing a functionality to the users where "They select a near future time, date while giving an Order. This Order time and date is stored in the database."
How can I trigger a function to perform something when the time and date in the database is reached?
I am going to use Node JS, Express JS, Mongo DB, React JS for the backend, server, database, and frontend respectively. I am asking for the architecture or creating such trigger of how this can be performed and not for complete code.
Any supporting NPM packages recommendation would be helpful. Thank you.
Note: This is my first application development in any language. I have learnt these technologies only from Udemy courses. The application will have Users select a date in the future and when the future date is reached, the application does some function for them.
javascript node.js reactjs mongodb triggers
I am providing a functionality to the users where "They select a near future time, date while giving an Order. This Order time and date is stored in the database."
How can I trigger a function to perform something when the time and date in the database is reached?
I am going to use Node JS, Express JS, Mongo DB, React JS for the backend, server, database, and frontend respectively. I am asking for the architecture or creating such trigger of how this can be performed and not for complete code.
Any supporting NPM packages recommendation would be helpful. Thank you.
Note: This is my first application development in any language. I have learnt these technologies only from Udemy courses. The application will have Users select a date in the future and when the future date is reached, the application does some function for them.
javascript node.js reactjs mongodb triggers
javascript node.js reactjs mongodb triggers
asked Jan 2 at 6:59


Sainath ASainath A
31
31
try using moment js
– the_ultimate_developer
Jan 2 at 7:01
usesetInterval
but limitation is that the page should not be reloaded. Or the other way you can create an interval after reloading page.
– Sameer Reza Khan
Jan 2 at 7:08
add a comment |
try using moment js
– the_ultimate_developer
Jan 2 at 7:01
usesetInterval
but limitation is that the page should not be reloaded. Or the other way you can create an interval after reloading page.
– Sameer Reza Khan
Jan 2 at 7:08
try using moment js
– the_ultimate_developer
Jan 2 at 7:01
try using moment js
– the_ultimate_developer
Jan 2 at 7:01
use
setInterval
but limitation is that the page should not be reloaded. Or the other way you can create an interval after reloading page.– Sameer Reza Khan
Jan 2 at 7:08
use
setInterval
but limitation is that the page should not be reloaded. Or the other way you can create an interval after reloading page.– Sameer Reza Khan
Jan 2 at 7:08
add a comment |
2 Answers
2
active
oldest
votes
You can make use of the following package to achieve your goal: node-schedule
For example:
var schedule = require('node-schedule');
var date = // get date from databse and create a date object for eg: new Date(2012, 11, 21, 5, 30, 0);
var j = schedule.scheduleJob(date, function(){
console.log('The world is going to end today.');
// once the job is finished remove the database entry mark it as completed so that it won't run again
});
Note:
on the startup, you can also add validation to check the date of your jobs and schedule if the date is in future or invoke the method if it already past and not completed, this is helpful when the application is deployed
This seems to be the easiest and working solution. Thank you!
– Sainath A
Jan 4 at 9:22
So I would need to start the scheduleJob as soon as I get the order right?
– Sainath A
Jan 4 at 9:40
Yes, you have to pass the date of the order and it will automatically trigger on that date.
– Piyush Zalani
Jan 4 at 10:24
If the deployed server restarts at a time between the schedule and method invoked, the job will not be able to complete as it will be removed from the memory right?
– Sainath A
Feb 26 at 6:05
add a comment |
Create a collection for orders:
const orders = db.collection("orders");
Then create an index on the due date for faster indexing:
orders.createIndex({ dueDate: 1 });
Now you can start a service to take the lowest date, and check wether that was reached already, otherwise sleep:
const sleep = ms => new Promise(res => setTimeout(res, ms));
(async function() {
while(true) { // check forever
await sleep(2000);
const cursor = orders.find({ dueDate: { $lte: Date.now() }, processed: false }); // only take those that are done but not yet processed
while(await cursor.hasNext()) {
const order = await cursor.next();
// process order
await orders.updateOne(order, { $set: { processed: true }});
}
}
})();
Then just add new documents to that collection and watch the magic :)
orders.insertOne({ processed: false, dueDate: Date.now() + 5000 });
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002431%2fhow-to-trigger-events-in-node-js-when-a-future-date-time-stored-in-database-is%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can make use of the following package to achieve your goal: node-schedule
For example:
var schedule = require('node-schedule');
var date = // get date from databse and create a date object for eg: new Date(2012, 11, 21, 5, 30, 0);
var j = schedule.scheduleJob(date, function(){
console.log('The world is going to end today.');
// once the job is finished remove the database entry mark it as completed so that it won't run again
});
Note:
on the startup, you can also add validation to check the date of your jobs and schedule if the date is in future or invoke the method if it already past and not completed, this is helpful when the application is deployed
This seems to be the easiest and working solution. Thank you!
– Sainath A
Jan 4 at 9:22
So I would need to start the scheduleJob as soon as I get the order right?
– Sainath A
Jan 4 at 9:40
Yes, you have to pass the date of the order and it will automatically trigger on that date.
– Piyush Zalani
Jan 4 at 10:24
If the deployed server restarts at a time between the schedule and method invoked, the job will not be able to complete as it will be removed from the memory right?
– Sainath A
Feb 26 at 6:05
add a comment |
You can make use of the following package to achieve your goal: node-schedule
For example:
var schedule = require('node-schedule');
var date = // get date from databse and create a date object for eg: new Date(2012, 11, 21, 5, 30, 0);
var j = schedule.scheduleJob(date, function(){
console.log('The world is going to end today.');
// once the job is finished remove the database entry mark it as completed so that it won't run again
});
Note:
on the startup, you can also add validation to check the date of your jobs and schedule if the date is in future or invoke the method if it already past and not completed, this is helpful when the application is deployed
This seems to be the easiest and working solution. Thank you!
– Sainath A
Jan 4 at 9:22
So I would need to start the scheduleJob as soon as I get the order right?
– Sainath A
Jan 4 at 9:40
Yes, you have to pass the date of the order and it will automatically trigger on that date.
– Piyush Zalani
Jan 4 at 10:24
If the deployed server restarts at a time between the schedule and method invoked, the job will not be able to complete as it will be removed from the memory right?
– Sainath A
Feb 26 at 6:05
add a comment |
You can make use of the following package to achieve your goal: node-schedule
For example:
var schedule = require('node-schedule');
var date = // get date from databse and create a date object for eg: new Date(2012, 11, 21, 5, 30, 0);
var j = schedule.scheduleJob(date, function(){
console.log('The world is going to end today.');
// once the job is finished remove the database entry mark it as completed so that it won't run again
});
Note:
on the startup, you can also add validation to check the date of your jobs and schedule if the date is in future or invoke the method if it already past and not completed, this is helpful when the application is deployed
You can make use of the following package to achieve your goal: node-schedule
For example:
var schedule = require('node-schedule');
var date = // get date from databse and create a date object for eg: new Date(2012, 11, 21, 5, 30, 0);
var j = schedule.scheduleJob(date, function(){
console.log('The world is going to end today.');
// once the job is finished remove the database entry mark it as completed so that it won't run again
});
Note:
on the startup, you can also add validation to check the date of your jobs and schedule if the date is in future or invoke the method if it already past and not completed, this is helpful when the application is deployed
answered Jan 2 at 7:28


Piyush ZalaniPiyush Zalani
889318
889318
This seems to be the easiest and working solution. Thank you!
– Sainath A
Jan 4 at 9:22
So I would need to start the scheduleJob as soon as I get the order right?
– Sainath A
Jan 4 at 9:40
Yes, you have to pass the date of the order and it will automatically trigger on that date.
– Piyush Zalani
Jan 4 at 10:24
If the deployed server restarts at a time between the schedule and method invoked, the job will not be able to complete as it will be removed from the memory right?
– Sainath A
Feb 26 at 6:05
add a comment |
This seems to be the easiest and working solution. Thank you!
– Sainath A
Jan 4 at 9:22
So I would need to start the scheduleJob as soon as I get the order right?
– Sainath A
Jan 4 at 9:40
Yes, you have to pass the date of the order and it will automatically trigger on that date.
– Piyush Zalani
Jan 4 at 10:24
If the deployed server restarts at a time between the schedule and method invoked, the job will not be able to complete as it will be removed from the memory right?
– Sainath A
Feb 26 at 6:05
This seems to be the easiest and working solution. Thank you!
– Sainath A
Jan 4 at 9:22
This seems to be the easiest and working solution. Thank you!
– Sainath A
Jan 4 at 9:22
So I would need to start the scheduleJob as soon as I get the order right?
– Sainath A
Jan 4 at 9:40
So I would need to start the scheduleJob as soon as I get the order right?
– Sainath A
Jan 4 at 9:40
Yes, you have to pass the date of the order and it will automatically trigger on that date.
– Piyush Zalani
Jan 4 at 10:24
Yes, you have to pass the date of the order and it will automatically trigger on that date.
– Piyush Zalani
Jan 4 at 10:24
If the deployed server restarts at a time between the schedule and method invoked, the job will not be able to complete as it will be removed from the memory right?
– Sainath A
Feb 26 at 6:05
If the deployed server restarts at a time between the schedule and method invoked, the job will not be able to complete as it will be removed from the memory right?
– Sainath A
Feb 26 at 6:05
add a comment |
Create a collection for orders:
const orders = db.collection("orders");
Then create an index on the due date for faster indexing:
orders.createIndex({ dueDate: 1 });
Now you can start a service to take the lowest date, and check wether that was reached already, otherwise sleep:
const sleep = ms => new Promise(res => setTimeout(res, ms));
(async function() {
while(true) { // check forever
await sleep(2000);
const cursor = orders.find({ dueDate: { $lte: Date.now() }, processed: false }); // only take those that are done but not yet processed
while(await cursor.hasNext()) {
const order = await cursor.next();
// process order
await orders.updateOne(order, { $set: { processed: true }});
}
}
})();
Then just add new documents to that collection and watch the magic :)
orders.insertOne({ processed: false, dueDate: Date.now() + 5000 });
add a comment |
Create a collection for orders:
const orders = db.collection("orders");
Then create an index on the due date for faster indexing:
orders.createIndex({ dueDate: 1 });
Now you can start a service to take the lowest date, and check wether that was reached already, otherwise sleep:
const sleep = ms => new Promise(res => setTimeout(res, ms));
(async function() {
while(true) { // check forever
await sleep(2000);
const cursor = orders.find({ dueDate: { $lte: Date.now() }, processed: false }); // only take those that are done but not yet processed
while(await cursor.hasNext()) {
const order = await cursor.next();
// process order
await orders.updateOne(order, { $set: { processed: true }});
}
}
})();
Then just add new documents to that collection and watch the magic :)
orders.insertOne({ processed: false, dueDate: Date.now() + 5000 });
add a comment |
Create a collection for orders:
const orders = db.collection("orders");
Then create an index on the due date for faster indexing:
orders.createIndex({ dueDate: 1 });
Now you can start a service to take the lowest date, and check wether that was reached already, otherwise sleep:
const sleep = ms => new Promise(res => setTimeout(res, ms));
(async function() {
while(true) { // check forever
await sleep(2000);
const cursor = orders.find({ dueDate: { $lte: Date.now() }, processed: false }); // only take those that are done but not yet processed
while(await cursor.hasNext()) {
const order = await cursor.next();
// process order
await orders.updateOne(order, { $set: { processed: true }});
}
}
})();
Then just add new documents to that collection and watch the magic :)
orders.insertOne({ processed: false, dueDate: Date.now() + 5000 });
Create a collection for orders:
const orders = db.collection("orders");
Then create an index on the due date for faster indexing:
orders.createIndex({ dueDate: 1 });
Now you can start a service to take the lowest date, and check wether that was reached already, otherwise sleep:
const sleep = ms => new Promise(res => setTimeout(res, ms));
(async function() {
while(true) { // check forever
await sleep(2000);
const cursor = orders.find({ dueDate: { $lte: Date.now() }, processed: false }); // only take those that are done but not yet processed
while(await cursor.hasNext()) {
const order = await cursor.next();
// process order
await orders.updateOne(order, { $set: { processed: true }});
}
}
})();
Then just add new documents to that collection and watch the magic :)
orders.insertOne({ processed: false, dueDate: Date.now() + 5000 });
edited Jan 2 at 7:47
answered Jan 2 at 7:28
Jonas WilmsJonas Wilms
62.4k53456
62.4k53456
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002431%2fhow-to-trigger-events-in-node-js-when-a-future-date-time-stored-in-database-is%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
try using moment js
– the_ultimate_developer
Jan 2 at 7:01
use
setInterval
but limitation is that the page should not be reloaded. Or the other way you can create an interval after reloading page.– Sameer Reza Khan
Jan 2 at 7:08