0

I am trying to write some items I pushed into an array into a JSON file in node.js but I can't figure out how to wait for the array to contain the items before writing the JSON file. As a result the file is always empty. Do i need to have a callback? If so, how? NB:I'm still new to node.js

This is the code below:

    var getLinks = require('./news_archive/news_links.js');
    var request = require('request');
    var cheerio = require('cheerio');
    var fs = require('fs');
    var saveNews = './news_archive/news.json';
    var jsonObj = [];
    var i;
    var number_of_links = getLinks.links.length;


    for(i=0; i<number_of_links; i++){

        //GET ARTICLE LINK FROM link.js
        var url = "http://www.times.co.sz/"+getLinks.links[i];

        request(url, function(err, resp, body){
        var $ = cheerio.load(body);

        //GET ARTICLE HEADLINE
        var storyHeadline = $('#article_holder h1');
        var storyHeadlineText = storyHeadline.text();

        //GET DATE POSTED
        var datePosted = $('.metadata_time');
        var datePostedText = datePosted.text();

        //GET ARTICLE REPORTER'S NAME
        var reporterName = $('.article_metadata a');
        var reporterNameText = reporterName.text();

        //GET ARTICLE SUMMARY
        var fullStory = $('#article_body span');
        var fullStoryText = fullStory.text();

        //PUSH ITEMS TO jsonObj ARRAY 

            jsonObj.push({
                id: i,
                storyHeadline: storyHeadlineText,
                datePosted: datePostedText,
                reporterName: reporterNameText,
                fullStory: fullStoryText
        })

        });

    } //END for LOOP

        //WRITE TO news.json file
        fs.writeFile(saveNews, JSON.stringify(jsonObj, null, 4), function(err) {
        if(err) {
        console.log(err);
        } else {
        console.log("JSON saved to " + saveNews);
        }
        });
1
  • request is async. By the time you are writing a file requests are not fulfilled yet. Commented Jul 15, 2016 at 7:27

1 Answer 1

1

The issue is that request is asyncronous and you cannot use syncronous loop to iterate through. You can use async lib for that

var getLinks = require('./news_archive/news_links.js');
var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
var saveNews = './news_archive/news.json';
var number_of_links = getLinks.links.length;
var async = require('async');


async.times(number_of_links, function (i, next) {
    var url = "http://www.times.co.sz/"+getLinks.links[i];

    request(url, function(err, resp, body){
        var $ = cheerio.load(body);

        //GET ARTICLE HEADLINE
        var storyHeadline = $('#article_holder h1');
        var storyHeadlineText = storyHeadline.text();

        //GET DATE POSTED
        var datePosted = $('.metadata_time');
        var datePostedText = datePosted.text();

        //GET ARTICLE REPORTER'S NAME
        var reporterName = $('.article_metadata a');
        var reporterNameText = reporterName.text();

        //GET ARTICLE SUMMARY
        var fullStory = $('#article_body span');
        var fullStoryText = fullStory.text();

        //PUSH ITEMS TO jsonObj ARRAY

        next(err, {
            id: i,
            storyHeadline: storyHeadlineText,
            datePosted: datePostedText,
            reporterName: reporterNameText,
            fullStory: fullStoryText
        });

    });
}, function (err, res) {
    // do not forget to handle error
    fs.writeFile(saveNews, JSON.stringify(res, null, 4), function(err) {
        if(err) {
            console.log(err);
        } else {
            console.log("JSON saved to " + saveNews);
        }
    });
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!! I almost cracked my head with this. IT WORKS PERFECTLY!! I tried voting up but I got a reputation less than 15, sorry man.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.