0

In my application, I want to store an object for later execution of functions basically that object holds the data of cron job.

var cronJobObject = schedule.scheduleJob(new Date(2018, 0, 19, 15, 15, 0), function() {
    console.log("Cron started here");
});

And I am storing that object in mongodb like JSON.stringify(cronJobObject)

And retrieve with cronObj = JSON.parse(obj)

but when I call a function on that object I get function undefined error

I think my problem is similar to this node.js store objects in redis

But the solution didn't help me

1 Answer 1

0

Because JSON.stringify(obj) will not fully serialize your object and when you parse it with JSON.parse(obj) you won't get the full initial object:

you can test it with a simple code like this:

var schedule = require('node-schedule');


var j = schedule.scheduleJob('43 * * * *', function() {
    console.log('hello!!!!');
});


x = JSON.stringify(j)

console.log(j);
console.log(x);
console.log(JSON.parse(x));

which results:

<Job {
  job: [Function],
  callback: false,
  name: '<Anonymous Job 1>',
  trackInvocation: [Function],
  stopTrackingInvocation: [Function],
  triggeredJobs: [Function],
  setTriggeredJobs: [Function],
  cancel: [Function],
  cancelNext: [Function],
  reschedule: [Function],
  nextInvocation: [Function],
  pendingInvocations: [Function] }



{"callback":false,"name":"<Anonymous Job 1>"}
{ callback: false, name: '<Anonymous Job 1>' }
Sign up to request clarification or add additional context in comments.

2 Comments

So whats the solution
@iambatman well you can't store a function in mongoDB it only stores raw data so you can store your cron job parameters in mongo and then retrieve them and rebuild your cron function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.