I actually have a problem saving some data from an array in a mysql database with nodejs.
This is my code
for (var i = 0; i < data.data.length; i++) {
    var imageObject = data.data[i];
    var url = imageObject.images.standard_resolution.url;
    var id = imageObject.id;
    var sql = 'SELECT COUNT(*) AS imageIDCount FROM images WHERE id = ?'
    var ids
    connection.query(sql, [id], function(err, rows, fields) {
        console.log(rows[0].imageIDCount);
        if (err) throw err;
        if (rows[0].imageIDCount == 0) {
            console.log(id + " doesn't exist"); // ### the ID at this point is always the last from that array
            //insertImage(id, url);
        } else {
            // console.log("ID exists");
        }
    });
}
This code run's when I get a response from an rest-api with the request-framework.
So my problem is that at the point I get the result from the count-query and there is no element with the specific id I get always the same id. 
I think that's because I use the same variable "id" there but how can I fix it ? I hope somebody can help me.