Silvering
Silvering

Reputation: 787

Parsing json and write file

I'm facing a strange (at least for me) issue. I'm trying to fetch (download via wget) a json file, parse it and extract some content (images urls). This content is then stored in an array which is saved in a txt file. At the end I use wget again to download the list of images urls.

I use Electron.io to this app. An Ajax function enable to perform this nodejs function :

expressApp.get('/update', function(req, res) {
var util = require('util'),
    exec = require('child_process').exec,
    child, url1 = 'http://xxxxxx/api/product/';

exec = require('child_process').exec,
    child, url2 = 'http://xxxxxx/api/image/';

child = exec('wget ' + url1 + ' -O update.json',

    function(error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        console.log('Content updated!')
        if (error !== null) {
            console.log('exec error: ' + error);
        }
    });

child = exec('wget ' + url2 + ' -O image.json',

    function(error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        console.log('Image.json downloaded!')
        if (error !== null) {
            console.log('exec error: ' + error);
        }

    });

var file = __dirname + '/image.json';
var file_updated = __dirname + '/image_name.txt';

fs.readFile(file, 'utf8', function(err, data) {
    if (err) {
        console.log('Error: ' + err);
        return;
    }

    data = JSON.parse(data);

    var arr = data.objects.map(function(item) {
        return item.image;
    })

    var arr_updated = arr.toString();

    // console.log(arr);

    fs.writeFile(file_updated, '', function() {
        fs.writeFile(file_updated, arr_updated, function(err) {
            if (err) return console.log(err);
            console.log('List of image url added!');

            fs.readFile(file_updated, 'utf-8', function(err, data) {
                if (err) {
                    return console.log(err);
                }
                var result = data.replace(/,/g, '\n');

                fs.writeFile(file_updated, result, 'utf8', function(err) {
                    if (err) return console.log(err);
                });
            });
        });
    })
});

// child = exec('wget -i ' + file_updated + ' -P images', {maxBuffer: 1024 * 100000},

//   function (error, stdout, stderr) {
//     console.log('stdout: ' + stdout);
//     console.log('stderr: ' + stderr);
//     console.log('Images downloaded!')
//     if (error !== null) {
//       console.log('exec error: ' + error);
//     }

//   });

});

My issue is that when I execute once this script, image_text.txt is NOT updated, when I execute it again is updated.. I tried to understand why but no idea... If someone could help me.

In advance thanks a lot.

Upvotes: 0

Views: 521

Answers (1)

vmkcom
vmkcom

Reputation: 1668

All your functions are asynchronous. It means, that you need to wait for finishing previous function before running next one

So you code should be looking like this

child = exec('wget ' + url1 + ' -O update.json',

    function(error, stdout, stderr) {
        // ...
        child = exec('wget ' + url2 + ' -O image.json',

            function(error, stdout, stderr) {
                //...

            }
        )
    }
)

and so on. You need to download files before you can process them. exec fires callback when your wget process done

For dealing with callback hell you can use https://github.com/caolan/async package

Upvotes: 1

Related Questions