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

Waleed Iqbal
  • 1,308
  • 19
  • 35
shamon shamsudeen
  • 5,466
  • 17
  • 64
  • 129

1 Answers1

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>' }
NaWeeD
  • 561
  • 5
  • 15