0

i am new to nodejs and i am trying to form an array of products which are having invalid imageUrls by using "http.get", I am querying my products from my mongodb collection, though i used "Promise" the array is printing first, i am not getting the result form the "hh.get"

here is my code in server side

    var Promise = require('bluebird'),
         hh = require('http-https')
         mongoose = require('mongoose'),
         collection = mongoose.model('Collection');


    function getInValidImgUrl(prodObj){
     return hh.get(prodObj.imageUrl,function(res){
         if(res.statusCode == 404){
            return {
                    name:prodObj.name,
                    imgUrl:prodObj.imageUrl 
                   }
          }
      })
    }


    exports.sampleFunc=function(){
    collection.find({category:"electronics"}).exec(function(err,products){
     if(err){
      console.log(err)
      }else{
        var imgArr=[];
//eg:products=[{name:"mobile",imageUrl:"http://somepic.jpg"}]

       for(var i=0; i<products.length: i++){
           imgArr.push(getInValidImgUrl(products(i)));
        }

       Promise.all(imgArr).then(results =>{
          console.log("IAMGE ARRAY :"+JSON.stringify(results)); //here iam not getting array
        })

      }
    });
    }

thanks in advance.

Rahul Kumar
  • 2,781
  • 1
  • 21
  • 29
Midhunsai
  • 427
  • 8
  • 26

1 Answers1

1

You don't actually need to use bluebird for this, although you could use the npm package request-promise (https://www.npmjs.com/package/request-promise) I use that quite a lot. In the interests of not changeing what you have too much your issue is that you are making the return in the callback for the getInValidImgUrl function. You can change this to use the standard Promise class that comes for free with node (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)

function getInValidImgUrl(prodObj){
     return new Promise((resolve,reject) => {
         hh.get(prodObj.imageUrl,function(res){
         if(res.statusCode == 404){
            resolve( {
                    name:prodObj.name,
                    imgUrl:prodObj.imageUrl 
                   })
          }
      })
}
}
Peter Grainger
  • 4,539
  • 1
  • 18
  • 22
  • thanks for giving rply, while iam executing with your code it is give error like this `Hostname/IP doesn't match certificate's altnames: "Host: touchofmodern.insnw.net` @Peter Grainger – Midhunsai Dec 30 '16 at 10:51
  • Is your server configured for TLS? Maybe this answer will help you: https://stackoverflow.com/questions/14088787/hostname-ip-doesnt-match-certificates-altname – Peter Grainger Dec 30 '16 at 10:59
  • @Midhunsai ping – Peter Grainger Dec 30 '16 at 11:00
  • the reason for that error is some of the images urls are not displaying image instead of they are redirecting to their websites so it is asking me some certificate related stuff @Peter Grainger – Midhunsai Dec 30 '16 at 11:46
  • hrm... so what you are saying is that you are trying to find all invalid image urls and you were listening for a 404 reply, however the server does a redirect (302?) but the page it redirects to errors because you can't connect to that host? – Peter Grainger Dec 30 '16 at 12:01
  • when returned the object from the `getInValidImgUrl` function i want to print the "imgArr" in console it is not printing final array can you please solve this @Peter Grainger – Midhunsai Dec 30 '16 at 13:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131909/discussion-between-midhunsai-and-peter-grainger). – Midhunsai Dec 30 '16 at 13:27