I've been puzzling over this loop for a few days and can't quite get to what I need.
I loop over my array of search results (mdb_results) and extract from each object a .name to use as a _search term in a Google CSE Image Search.
Th CSE returns another array (cse) which I want to append to the object from which I extracted the _search term (mdb_results[i]).
router.get('/json', function(req, res, next) {
var ImageSearch = require('node-google-image-search');
MongoClient.connect(url, function(err,db){
  db.collection('mycatalog')
    .find({$text: {$search:"FOO" }})
    .toArray(function(err, mdb_results){    
        for (var i=0; i<mdb_results.length; i++){           
            var _search =  mdb_results[i].name ;
            ImageSearch(_search, function(cse){
               // How to add cse.img to mdb_results[i].images ??
               //    mdb_results[i].images = cse;
               // gives undefined
            },0,2);  
        };
        res.send(mdb_results);
        });
    });
});
My initial mdb_results looks like this.
[{"name":"FOO"},{"name":"FOOOO"}]
I'm trying to achieve something like this,
[{"name":"FOO", 
  "images":[{"img1":"http://thumbnail1"},{"img2":"http://thumbnail2"}]
},
{"name":"FOOOO", 
  "images":[{"img1":"http://thumbnaila"},{"img2":"http://thumbnailb"}]
}]
Can anyone show me how to achieve this?
Thanks
via Colin
No comments:
Post a Comment