0

I am trying to use variables "hrs", "min", "sec" to perform a cron job but it does not seem to work correctly.

const express = require('express');
const router = express.Router();
var cron = require('node-cron');

router.get('/schedulesms',async(req,res)=>{
const {hrs,min,sec} = req.body ;


cron.schedule(`/${sec} /${min} /${hrs} * * *`, () => {
  console.log('running a task according to given hrs min sec ');
});

res.send("All okk!!!!");

})

module.exports = router ;

I am getting following error after hitting my API.

 throw new Error(`${patterns[0]} is a invalid expression for second`);
[1]               ^
[1]
[1] Error: /5 is a invalid expression for second

after hitting api

  • It should be `*/${min} */${hrs} * * *`. Cron doesn't support seconds. – kelsny Feb 27 '23 at 20:13
  • cron.schedule(`* /${min} /${hrs} * * *`, () => { console.log('running a task according to given hrs min sec '); });- I have changed my code as this but there is still an error as: throw new Error(`${patterns[1]} is a invalid expression for minute`); [1] ^ [1] [1] Error: /0 is a invalid expression for minute – Akshit Tomar Feb 28 '23 at 01:53
  • You aren't even using the pattern I sent... – kelsny Feb 28 '23 at 02:36

0 Answers0