I'm attempting to increment a variable by utilizing data returned in the response of an http GET request. I declare a variable globally with var nextTimestamp
, grab data from the http response with nextTimestamp = images[0].created_time
, and then attempt to increment minTimestamp
by placing minTimestamp = nextTimestamp
just outside the close of my http request. Problem is, outside of the response, nextTimestamp
keeps coming back as undefined
.
Relevant Node server code is below:
var minTimestamp = 1419656400;
var nextTimestamp;
request('https://api.instagram.com/v1/media/search?lat=40.8296659&lng=-73.9263128&distance=250&min_timestamp=' + minTimestamp + '&client_id=CLIENT-ID',
function (error, response, body) {
if (error) {
console.log('error');
return;
}
//JSON object with all the info about the image
var imageJson = JSON.parse(body);
var images = imageJson.data;
nextTimestamp = images[0].created_time;
var numImages = images.length;
async.eachSeries(images, function(image, callback) {
//Save the new object to DB
Event.findOneAndUpdate( { $and: [{latitude: '40.8296659'}, {radius: '250'}] }, { $push: {'photos':
{ img: image.images.standard_resolution.url,
link: image.link,
username: image.user.username,
profile: image.user.profile_picture,
text: image.caption ? image.caption.text : '',
longitude: image.location.longitude,
latitude: image.location.latitude
}}},
{ safe: true, upsert: false },
function(err, model) {
console.log(err);
}
);
callback();
}, function(err){
// if any of the image processing produced an error, err would equal that error
if( err ) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('Image failed to process');
} else {
console.log('Images processed');
}
});
}
);
minTimestamp = nextTimestamp;