0

I have a file to handle captcha solving, in captcha.js there is a variable module.exports.taskCaptchas = [] In consortium.js I have const captcha = require("./captcha.js") When I try to retrieve a value in consortium.js with captcha.taskCaptcha[0] it returns undefined but I have confirmed that there is a value in there. What am I doing wrong?

Captcha.js

module.exports.taskCaptchas = [];

ipcMain.on("updateCaptchaQueue", (event, token) => {

            global.captchaQueue.shift();
            console.log('New captcha token received. Captcha token: ' + token)
      console.log("Sending to task")
      module.exports.taskCaptchas.push(token)
            console.log(module.exports.taskCaptchas[0])
      console.log(module.exports.taskCaptchas[0] == token)

            if (global.captchaQueue.length >= 1) {
                module.exports.requestCaptcha(global.captchaQueue[0], false)
            } else {
                module.exports.capWin.hide();
            }
        });

Consortium.js

const captcha = require('./captcha.js')

function solveCap(productID, formKey, attributeID){
        ipcRenderer.send("captcha","Consortium")
        const capHandler = () => {
        console.log(captcha.taskCaptchas[0])
        if (captcha.taskCaptchas[0] == undefined || captcha.taskCaptchas[0] == '') {
        setTimeout(() => capHandler(), 1000);
        }else{
          var response = captcha.taskCaptchas[0];
          setTimeout(atc, delayTime, productID, formKey, attributeID, response);
          return;
        }
        }
        capHandler();
      }
Kyle Osborne
  • 13
  • 1
  • 4

1 Answers1

-1

I tried this in order to validate your module definition (Basically it's the same but without the functions):


    // taskcaptchas.js
    const captchas = [];
    setInterval(() => {
        captchas.push("token");
    }, 3000);

    module.exports = { captchas };


    //consortium.js
    const taskCaptchas = require("./taskcaptchas.js");

    setInterval(() => {     
        console.log(taskCaptchas.captchas); 
    }, 1000); 

This is the output:

Program execution

I think that you need to give it more time on setTimeout(() => capHandler(), 1000); in order to wait until taskCaptcha array will populated by ipcMain.on("updateCaptchaQueue", event. let's give it 2 or 3 seconds.

  • I can see that the array has updated though through the checks I made in captcha.js but at the same time the captcha handler still shows undefined even seconds after the token is pushed to the array – Kyle Osborne Jun 27 '19 at 15:33