Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

312
Visualizações
How to Run a Cron Job when the code is in Other File - Node JS

I am trying to execute a cron every 1 hour.

For which I have initiated the cron job in my index.js file as below

const cron = require('node-cron');
const readDocuments = require("./cronJobs/readDocuments");
var task = cron.schedule('0 0 */1 * * *', readDocuments);

task.start();

Where as the actual code to be executed has been written in ./cronJobs/readDocuments and the code inside this file is below, where I am trying to read a csv file.

readDocuments.js

const ResponseManager = require("../common/ResponseManager");

var fs = require('fs');
const csv = require('csv-parser');
console.log('Read Document....')
try {

var filepath = "../files/abc.csv";
fs.createReadStream(filepath)
.on('error', () => {
// handle error
})
.pipe(csv())
.on('data', (row) => {
console.log('rowrowrow',row)
})
.on('end', () => {
// handle end of CSV
})
} catch (error) {
console.log('errorerror',error)
res.status(500).json(ResponseManager(false, error.message));
}

When I run the node js in cmd with node index.js, the console Read Document.... is getting displayed but the other code is not getting executed. It is giving me throw 'execution must be a function'; error

How do I resolve this or what is the actual procedure to execute the code inside a file with cron job from index.js.

Please help. Thanks !!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you need to export the cron function and consume it on your file

about 4 years ago · Juan Pablo Isaza Relatório

0

The second argument to cron.schedule() must be function. You need to wrap the code into a function and export it from the module.

const ResponseManager = require("../common/ResponseManager");
const fs = require('fs');
const csv = require('csv-parser');

module.exports = function () {
  console.log('Read Document....')
  try {
    const filepath = "../files/abc.csv";
    fs.createReadStream(filepath)
      .on('error', () => {
        // handle error
      })
      .pipe(csv())
      .on('data', (row) => {
        console.log('rowrowrow', row)
      })
      .on('end', () => {
        // handle end of CSV
      });
  } catch (error) {
    console.log('errorerror', error)
    res.status(500).json(ResponseManager(false, error.message));
  }
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda