0

I am assigning a variable, proxies, equal to an array. I am trying to add items to that array at a later point in time and then access that array in other files.

The problem is, the proxyHelper.proxies value is not updating to reflect the value of proxies variable.

After modifying the array, console.log(proxies) returns the modified array, but console.log(proxyHelper.proxies) returns a blank. I need to access the proxyHelper.proxies value in other files, so you can see why this is a problem.

I use similar code elsewhere and it works fine - what am I not seeing?

var proxies = [];

proxyHelper.proxies = proxies;

proxyHelper.tester = function(win) {

    electron.ipcMain.on('saveProxies', function(event, data) {
        // Clear the previous proxies from list
        proxies = [];
        // Split proxies based on line breaks
        if (data != '') {
            let proxiesList = data.split('\n');
            // i<= because we want to run a check to see if all proxies are added
            for (let i = 0; i <= proxiesList.length; i++) {
                // if the for loop hasn't ran through all proxies
                if (i + 1 <= proxiesList.length) {
                    proxies.push(proxiesList[i]);
                }
                // Once it's loop through all proxies
                else {
                    //Returns nothing
                    console.log(proxyHelper.proxies);
                    //Returns array with added items
                    console.log(proxies);
                    win.webContents.send('goodProxies', 'Saved!');
                }
            }
        } else {
            win.webContents.send('emptyProxies', 'Empty.');
        }
    })
}

3 Answers 3

2
proxies = [];

You just assigned a new array to this variable.

proxyHelper.proxies still points to the previous value, and is not affected.

You should always use a single variable, or mutate it instead of re-assigning it.

Sign up to request clarification or add additional context in comments.

Comments

1

Here is what is going on in your code:

var proxies = []; // This new variable ("proxies") contains a reference to an empty array.

proxyHelper.proxies = proxies; // You assign the same reference to a property 
                               // (named "proxies") of the "proxyHelper"
                               // object. The "proxies" property points
                               // to the same empty array as the variable above.

proxyHelper.tester = function(win) {

    electron.ipcMain.on('saveProxies', function(event, data) {

        proxies = []; // You assign a new reference to the global variable
                      // "proxies", which points to a new empty array.
                      // At this point the reference assigned to
                      // "proxyHelper.proxies" is still the original
                      // empty array from line 1
        // ...
    }

    // ...
}

So when you access "proxyHelper.proxies", you are always accessing the original empty array the has never been modified...

What you should do is:

proxyHelper.proxies = []; // resets the object's property to a new empty array

Comments

0

Don't need proxy variable, as its changes aren't reflected to proxyHelper.proxies
Just use proxyHelper.proxies itself
Also i've cleaned up your code a little bit
There are plenty of Array methods you can use, instead of for loop

proxyHelper.proxies = []

proxyHelper.tester = function(win) {

    electron.ipcMain.on('saveProxies', function(event, data) {
        //Split proxies based on line breaks
        if (data != '') {
            let proxiesList = data.split('\n');
            proxiesList.forEach(function(proxy) {
                proxyHelper.proxies.push(proxy)
            })
            win.webContents.send('goodProxies', 'Saved!');
        } else {
            win.webContents.send('emptyProxies', 'Empty.');
        }
    })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.