Carol.Kar
Carol.Kar

Reputation: 5215

Adding Data to Event Emitter

I am using "proxy-lists": "^1.16.0" package to obtain proxies.

I would like to save all incoming Array-Objects into my own array to later save it to the db.

When running the below example my array is empty and no file is written:

const ProxyLists = require('proxy-lists');
const fs = require('fs');

global.__basedir = __dirname;

const options = {
    countries: null
};

// `gettingProxies` is an event emitter object.
const gettingProxies = ProxyLists.getProxies(options);

const data = []
gettingProxies.on('data', function (proxies) {
    console.log(proxies);
    data.push(proxies)
});

gettingProxies.on('error', function (error) {
    console.error(error);
});

gettingProxies.once('end', function () {
    fs.writeFile(__basedir + "data/file.txt", data, function (err) {
        if (err) {
            return console.log(err);
        }
        console.log("The file was saved!");
    });
});

Any suggestions what I am doing wrong?

I appreciate your replies!

Upvotes: 1

Views: 62

Answers (1)

ChiralMichael
ChiralMichael

Reputation: 1224

Looks good to me, but when I tested locally, I see that there was an issue concatenating your paths. Try __basedir + "/data/file.txt" (or use path.join)

Upvotes: 1

Related Questions