0
static async updatePaidLeave() {
    const result = await PaidUnpaidLeaveSchema.find({});
    console.log(result, ";;;;;;;");
  }

  static runCronJob() {
    cron.schedule("*/10 * * * * *", async () => {
      this.updatePaidLeave();
      console.log(this.updatePaidLeave(), "Im running in every 10 seconds.");
    });
  }

Output // Promise { } Im running in every 10 seconds.

i have tried above code and i want data from PaidUnpaidLeaveSchema

Reyaj Alam
  • 21
  • 2
  • 1
    Did you try `await this.updatePaidLeave()` in `runCronJob` ? – mickl Nov 02 '22 at 11:09
  • 1
    are you aware that you are calling the function `updatePaidLeave` twice? – Guerric P Nov 02 '22 at 11:12
  • Yes i have tried "await this.updatePaidLeave()" but don't get any response – Reyaj Alam Nov 02 '22 at 11:37
  • Because `updatePaidLeave` does not return any value. – VLAZ Nov 02 '22 at 11:39
  • static async updatePaidLeave() { const result = await PaidUnpaidLeaveSchema.find({}); console.log(result); return result; } okay please anyone can tell me how I can get collection data in that function – Reyaj Alam Nov 02 '22 at 11:54
  • how can i get data in that function `````` static async updatePaidLeave() { const result = await PaidUnpaidLeaveSchema.find(); console.log(result); return result; } `````````````` the data is present in PaidUnpaidLeaveSchema collection – Reyaj Alam Nov 02 '22 at 12:10

1 Answers1

0

The code requires a couple of fixes. Try this (look at comments):

static async updatePaidLeave() {
   const result = await PaidUnpaidLeaveSchema.find({});
   console.log(result, ";;;;;;;");
   return result // return result
}
static runCronJob() {
    cron.schedule("*/10 * * * * *", async () => {
      const result = await this.updatePaidLeave(); // await and save result in a variable
      console.log(result, "Im running in every 10 seconds."); // log result
    });
  }
radar155
  • 1,796
  • 2
  • 11
  • 28
  • `updatePaidLeave` doesn't return anything… – deceze Nov 02 '22 at 11:32
  • how can i get data in that function `````` static async updatePaidLeave() { const result = await PaidUnpaidLeaveSchema.find(); console.log(result); return result; } `````````````` the data is present in PaidUnpaidLeaveSchema collection – Reyaj Alam Nov 02 '22 at 12:10