Saturday, 13 May 2017

Export array with Node.js

I have a function which I am trying to export which is as follows:

exports.pictures = function() {
  var images =[];
  request('https://www.pinterest.com/search/pins/?q=newyork',function(err,res,body){


    var $ = cheerio.load(body);
    $('img','div.SearchPageContent').each(function(){
      var img = $(this).attr('src');

      images.push(img);

    });


  for (var i=images.length-1; i>=0; i--) {
    if (images[i].substring(0, 5) !== 'https') {
        images.splice(i, 1);
    }
  }

  for(var i=0; i < images.length; i++) {
   images[i] = images[i].replace(/236x/g, '564x');

  }
// console.log(images) will work
return images;
})
};
module.exports;

But when I try and access the function having downloaded the published module, the return statement doesn't return the images. If I log the images in the console they are returned to me but then I cant do anything with the images other than see them in the console in my new project.. any idea why the return statement wont work?

Project I am trying to access the functionality on:

var images = require('mymodule');


var pictures = images.pictures();
// when this runs nothing comes back if I just have the return statement
// when this runs and I log the images in the console from the export function I can see the logged images here but only because they are logging in the export function.
//console.log(pictures) returns undefined



via user

No comments:

Post a Comment