How to insert data to mongo synchronously (Nodejs, Express)
<p>I have a problem with inserting data to mongo db using node(express)</p>

<p>My code looks like this:</p>

<pre><code>router.get('/data/:section/:sort', function(req, res, next) {
//Deleting old data always before writing new
//Image.remove().exec();
var section = req.params.section;
var sort = req.params.sort;
//Link to Igmur API
var url = 'https://api.imgur.com/3/gallery/'+section+'/'+sort+'/1'; //1 at the end is used to get more than 60 images(gives only 60 without it)
    request.get({
    url: url,
    method: 'GET',
    headers: {
        'Authorization': 'Client-Id XXXXXXXXXXXXX'
    }}, function(e, r, body){
var metadata = JSON.parse(body);
for(var i = 0; i&lt;100; i++){
        var image = new Image(metadata.data[i])
        image.save(function(err, result){
        });
};res.render('index', { title: 'SearchAPI' });});});
</code></pre>

<p>The problem is that I get only about 20 objects inserted instead of 100 as u can see in the loop.
All because node just jump forward before the save method is completely done.
How can I fix this? Thank you in advance</p>