0

I'm starting Javascript and async programming and I'm stuck with this problem.

I want to collect data from a collection in MongoDB database and schedule/start a cron job with this data.

const startCronJobs = (addressesArray) => {
    for (idx in addressesArray) {
        cron.schedule(('* * * * * ', () => {
            console.log(addressesArray[idx]);
        })).start();
    }
}
const handleConnectedWallets = async () => {
    const docs = await walletModel.find({}); // Retrieve all docs in the collection
    return (docs.map((doc) => { return doc.address }))
}

handleConnectedWallets().then((res) => {
    startCronJobs(res) // If console.log(res) -> ['0x06t...gq', '0x08as...af']
})

And i don't understand why I have the following error:

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "execution must be a function".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

I obviously tried to catch and throw the error after the then() but got the same error.

I must miss something!

Thanks for your help

Tiene
  • 1
  • 1

1 Answers1

0

You have a pair of parens too many. console.log((x, y)) would only log y.

cron.schedule('* * * * * ', () => {
    console.log(addressesArray[idx]);
}).start();
lusc
  • 1,206
  • 1
  • 10
  • 19