When I run my server, after some queries, it throws the following error:
C:\Users\Me\Documents\Project\node_modules\mongodb\lib\mongo_client.js:415
throw err
^
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect EADDRINUSE 127.0.0.1:27017]
at Pool.<anonymous> (C:\Users\Me\Documents\Project\node_modules\mongodb-core\lib\topologies\server.js:329:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (C:\Users\Me\Documents\Project\node_modules\mongodb-core\lib\connection\pool.js:280:12)
at Connection.g (events.js:292:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (C:\Users\Me\Documents\Project\node_modules\mongodb-core\lib\connection\connection.js:187:49)
at Socket.g (events.js:292:16)
at emitOne (events.js:96:13)
It is totally random but it mostly happens after 1000-2000 queries.
The node.js code to query is as follows:
let mongo = require('mongodb');
let MongoClient = mongo.MongoClient;
let url = "mongodb://localhost:27017/mydb";
function queryMongo(configurations, callback) {
MongoClient.connect(url, (err, db) => {
if (err) throw err;
let query = {
name: {
$in: configurations
}
};
db.collection("configurations").find(query).toArray((err, results) => {
if (err) throw err;
callback(results);
db.close();
});
});
}
The queryMongo function is called as soon as the previous query is done. I tried deleting the lock files but it did not change anything. I copied the database to another computer but still the same problem.