Jhead45 of Covalence-Nashville
4/24/2018 - 5:11 PM

Parse a Result - Promise and writeFilewith Node

Using Result-Promise parse the result, map it to an object and stringify to write

const path = require('path');
const fs = require('fs');
const rp = require('request-promise');

const dataPath = path.join(__dirname, '/popular-articles.json');


rp('https://reddit.com/r/popular.json')
    .then((res) => {
        let resParsed = JSON.parse(res);
        let articles = resParsed.data.children.map(item => {
            return({
                title: item.data.title,
                url: item.data.url,
                author: item.data.author
            });
        });

         let redditArray = JSON.stringify(articles);
         fs.writeFile(dataPath, redditArray, (err) => {
              console.log(err);   
         })
    })
    .catch((err) => {
        console.log(err);
    });