Riushda
Riushda

Reputation: 33

Ajax get request not getting response after 5 calls

I'm trying to get some information from the mongodb server to the frontend with ajax get request. Everything works fine except that if i try to call 5 times the javascript function, i dont get any response from my local server.

I tried to put some console.log() in order to debug in the nodejs function, it appears that the 6th time i call the function, the nodejs function doesn't even run.

javascript ajax front end code :


function addLike(music){

     var request = new XMLHttpRequest();

     request.open('POST', '/AddLike', true);
     request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
     var myobj = {"music": music};
     request.send(JSON.stringify(myobj));

     setTimeout(function(){

        $.ajax({
          url: "/getAll",
          type: 'GET',
          dataType: 'json', // added data type
          success: function(res) {
            // => the 6th time i launch addLike(music), the function doesn't go there
             update(res);
          }
    });

     }, 200);

} 

nodejs function :


app.get("/getAll", function(req, res){

    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/";

    MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
        if (err) throw err;

        var dbo = db.db("SoundShare");

        var tab = dbo.collection('Musique');

        tab.find({}).toArray(function(err2, result) { 
            if (err2) throw err2;

            res.send(JSON.stringify(result));

            db.close();

        });
    });
});

As you can see on the image below, in the console of firefox the last get ajax request doesn't receive any response.

Valid XHTML. Valid XHTML.

It seems like it is a server side problem but i don't understand how to fix it.

Thank you in advance for your answer.

Upvotes: 0

Views: 96

Answers (1)

Julien
Julien

Reputation: 2319

Your problem is that you are creating a new connection inside your /getAll function and the default poolsize is 5 as you can see in the docs.

You should be creating the connection when your node.js app starts up and use that connection throughout the application instead of creating a new connection on each request.

Upvotes: 3

Related Questions