Search code examples
javascriptimportmodulecron

js imported function error "getUser not a function"


I have three files

  • BotJobActions.js

  • TestDate.js

  • CreateCron.js

The BotJobActions file creates a function called getUser that returns the user connected to a specific job, then exports the getUser along with a bunch of other functions.

const getUser = async (jobId) =>{
    await mongoConnect(process.env.DB_PWORD)
    try {
        const user = await User.findOne({pendingJobs:jobId})
        return user
    } catch (err) {
        console.log(err)
    }
    
}

module.exports = { newJob, getUserJobs, getUser, updateUserJob, destroyUserPendingJob, destroyUserCompletedJob, activateJob, deactivateJob, endJob }

TestDate defines a function called runBot which runs a bot Job. In runBot it also calls the getUser function, so I can make changes to a specific user. Then exports the function because it will be used in other files.

const { getUser } = require("../bot/botJobActions");


const runBot = async (todayJobs) =>{
    // await mongoConnect(process.env.DB_PWORD)
    
    for(const job of todayJobs){
        const clubPassword = decryptToken(job.clubPassword.token, job.clubPassword.iv)
        const user = await getUser(job.id)

        if(job.proxy){
            const proxyConfig = await getProxyConfig(user)
            if(proxyConfig.status === "no proxy") console.log("[-] Proxy Config Retrival Error/Running Without Proxy")
            // await startBot(member=job.member?job.member : null, proxy=proxyConfig.status === 'success'?proxyConfig:null, job.clubUsername, clubPassword, job.startTime, job.endTime, job.courseList, job.id)
            await console.log(member=job.member?job.member : null, proxy=proxyConfig.status === 'success'?proxyConfig:null, job.clubUsername, clubPassword, job.startTime, job.endTime, job.courseList, job.id)
        }else{
            // await startBot(member=job.member?job.member : null, proxy=null, job.clubUsername, clubPassword, job.startTime, job.endTime, job.courseList, job.id)
            await console.log(member=job.member?job.member : null, proxy=null, job.clubUsername, clubPassword, job.startTime, job.endTime, job.courseList, job.id)
        }
    }
    return
    
}

module.exports = { runBot, getJobs }

CreateCron is a function that runs whenever a job is created with a specific start time. This function will create a cron job for that specified time to run the bot.

const schedule = require('node-schedule');
const { runBot } = require('./testDate');

const createCron = (job) =>{
    const startDate = new Date(job.botStartDate)
    const startTime = new Date(`09/19/2000 ${job.botStartTime}`)

    startDate.setHours(startTime.getHours())
    startDate.setMinutes(startTime.getMinutes())


    console.log(startDate.toUTCString())
    schedule.scheduleJob(startDate, async function(){
        console.log('run job')
        await runBot([job])
    })
}

My problem thought is that whenever I run the createCron function, I get an error saying that the getUser is not a function. Even thought it is.

Any help is appreciated!!


Solution

  • I was able to fix the problem. All I had to do was use the absolute path to the function instead of the relative path. Then the functions worked. Hope this can help somebody!