node.js - how to form an object and push it into an array using http/https from the resultant of response -


i new nodejs , trying form array of products having invalid imageurls using "http.get", querying products mongodb collection, though used "promise" array printing first, not getting result form "hh.get"

here 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.

you don't need use bluebird this, although use npm package request-promise (https://www.npmjs.com/package/request-promise) use quite lot. in interests of not changeing have issue making return in callback getinvalidimgurl function. can change use standard promise class comes free 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                     })           }       }) } } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -