mongodb connection timeout when callback function after query is slow - node.js

I am working on the back-end of a web application. Server-side, node is used along with express and mongo (via mongoose).
The client can make a post request that does the following:
load the file location of some data from mongo
load the file and do some heavy work with it
send the results to the client
This is done by a function that looks like this:
function (req, res, next) {
// read relevant info from req object, e.g.
var id = req.body.id;
mySchema.findById(id, (err, doc) => {
fs.readFile(doc.location, function (err, data) {
// do some heavy work with data
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
var bodyContent = [];
// fill bodyContent
res.write(JSON.stringify(bodyContent));
res.end();
});
});
};
This works fine for small files, but when the files become large and the computations of the "heavy work" take longer to execute, mongo throws connection timeout errors:
Error: connection timeout
at Db.<anonymous> (<project path>/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:169:17)
at emitTwo (events.js:106:13)
at Db.emit (events.js:191:7)
at Server.listener (<project path>/node_modules/mongoose/node_modules/mongodb/lib/db.js:1798:14)
at emitOne (events.js:96:13)
at Server.emit (events.js:188:7)
at Server.<anonymous> (<project path>/node_modules/mongoose/node_modules/mongodb/lib/server.js:274:14)
at emitOne (events.js:96:13)
at Server.emit (events.js:188:7)
at Pool.<anonymous> (<project path>/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:335:12)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (<project path>/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:270:12)
at Connection.g (events.js:291:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (<project path>/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:185:10)
at Socket.g (events.js:291:16)
at emitNone (events.js:86:13)
at Socket.emit (events.js:185:7)
at Socket._onTimeout (net.js:339:8)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
I have tried to increase the time before timeout as described in other posts related to mongo timeout issues and as described here, but to no avail. What is the best approach to solve such an issue?
Update:
Looking into the problem more closely, a possible solution seems to be disconnecting Mongo at the start of the callback, e.g. via mongoose.connection.close() and re-connect just before the res.end() line with something like mongoose.connect(config). However, I have not found a way to do this such that it functions when multiple requests are coming in. Any thoughts on this?

Related

Connecting to MongoDb Atlas using mongoose

I am trying to connect to MongoDb Atlas using URL string provided by atlas as :
mongoose
.connect(
"mongodb+srv://user:#contactlistcluster-hk1w5.mongodb.net/test?retryWrites=true"
)
.then(() => {
console.log("Connected to database!");
})
.catch((error) => {
console.log("Connection failed!");
console.log(error);
});
But i am getting this error while running :
**TypeError: Cannot read property 'close' of undefined
at topology.connect (C:\dev\contact-api_bind\node_modules\mongoose\node_modules\mongodb\lib\operations\mongo_client_ops.js:412:16)
at ReplSet.<anonymous> (C:\dev\contact-api_bind\node_modules\mongoose\node_modules\mongodb\lib\topologies\replset.js:364:11)
at Object.onceWrapper (events.js:255:19)
at ReplSet.emit (events.js:160:13)
at C:\dev\contact-api_bind\node_modules\mongodb-core\lib\topologies\replset.js:631:23
at Server.<anonymous> (C:\dev\contact-api_bind\node_modules\mongodb-core\lib\topologies\replset.js:357:9)
at Object.onceWrapper (events.js:255:19)
at Server.emit (events.js:160:13)
at Pool.<anonymous> (C:\dev\contact-api_bind\node_modules\mongodb-core\lib\topologies\server.js:562:21)
at Pool.emit (events.js:160:13)
at Connection.<anonymous> (C:\dev\contact-api_bind\node_modules\mongodb-core\lib\connection\pool.js:316:12)
at Object.onceWrapper (events.js:255:19)
at Connection.emit (events.js:160:13)
at TLSSocket.<anonymous> (C:\dev\contact-api_bind\node_modules\mongodb-core\lib\connection\connection.js:245:50)
at Object.onceWrapper (events.js:255:19)
at TLSSocket.emit (events.js:160:13)
[nodemon] app crashed - waiting for file changes before starting...**
The issue got Solved. Actually my organization was using a proxy server to block the connection.
Worked perfectly fine with my personal machine.

Can't set headers after they are sent when trying to redirect after using Google's oauth2Client.getToken()

After receiving oauth2 tokens from google I am trying to redirect to an endpoint on my express server to make api requests.
This is the flow of the auth requests:
app.get('/api/presentation/getpresentation/:id', function(req, res, next){
req.session.user = req.user.id;
req.session.presentation_id = req.params.id;
res.redirect(google_auth_url)
});
app.get('/oauth2callback', function(req, res) {
const code = req.query.code;
oauth2Client.getToken(code, function(err, tokens) {
req.session.tokens = tokens;
if (!err) {
oauth2Client.setCredentials(tokens);
res.redirect(`/api/presentation/${req.session.presentation_id}`);
return;
}
res.status(500).send(err);
})
});
app.get('/api/presentation/:id', **do things with api consent**);
The error:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:489:11)
at ServerResponse.setHeader (_http_outgoing.js:496:3)
at ServerResponse.header (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:767:10)
at ServerResponse.location (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:884:15)
at ServerResponse.redirect (/Users/joe/school/solo-project/present/node_modules/express/lib/response.js:922:18)
at /Users/joe/school/solo-project/present/server/index.js:112:11
at /Users/joe/school/solo-project/present/node_modules/google-auth-library/lib/auth/oauth2client.js:154:5
at Request._callback (/Users/joe/school/solo-project/present/node_modules/google-auth-library/lib/transporters.js:106:7)
at Request.self.callback (/Users/joe/school/solo-project/present/node_modules/request/request.js:186:22)
at emitTwo (events.js:125:13)
at Request.emit (events.js:213:7)
at Request.<anonymous> (/Users/joe/school/solo-project/present/node_modules/request/request.js:1163:10)
at emitOne (events.js:115:13)
at Request.emit (events.js:210:7)
at IncomingMessage.<anonymous> (/Users/joe/school/solo-project/present/node_modules/request/request.js:1085:12)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1057:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
[nodemon] app crashed - waiting for file changes before starting...
Any help is greatly appreciated, I've been tearing my hair out on this one for a while!
Thanks!

unable to fix some error with mongodb

Here is the error message which i got while trying to run the project using gulp. I guess the problem is caused because of mongodb.
Gulp is running my app on PORT: 8000
events.js:160
throw er; // Unhandled 'error' event
^
MongoError: failed to connect to server [localhost:27017] on first connect
[MongoError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (C:\Users\H SHASHANK
KUMAR\Desktop\code\node_modules\mongodb-core\lib\topologies\server.js:336:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (C:\Users\H SHASHANK KUMAR\Desktop\code\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\H SHASHANK
KUMAR\Desktop\code\node_modules\mongodb-
core\lib\connection\connection.js:187:49)
at Socket.g (events.js:292:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1277:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
Below is the code which may contain the error.
var express = require('express')
mongoose = require('mongoose'); // creating reference for express
var db = mongoose.connect('mongodb://localhost/bookAPI'); //using mongoose
to connect to mongo db.
var Book = require('./models/bookModel'); //mongo db uees models to retrive
data.
var app = express(); // creating an instance of express to run app.
var port = process.env.PORT || 3000;
var bookRouter = express.Router();
bookRouter.route('/Books')
.get(function(req, res){
Book.find(function(err, books){
if(err)
console.log(err);
else
res.json(books);
});
});
app.use('/api', bookRouter);
app.get('/', function(req, res){
res.send('welcome to my API');
});
app.listen(port, function(){
console.log('Gulp is running my app on PORT: '+port);
});
I have tried searching for the error in other possible post on stack overflow but i didn't find any fix for my problem.

Sails.js, MongoDb: timeout when trying to connect; possible to use the connection pool?

Sails server:
Windows 10 64-bit
Sails.js 1.0.0-36
Node.js 6.10.2
MongoDB server:
Centos 7 on Virtualbox, host networking only
Mongodb 3.4.4, listening to all interfaces, no auth configured
I can successfully lift Sails, connect the datastore, and use the models without any problem.
However, this test will result on a timeout:
let mongo = require('mongodb');
let uri = 'mongodb://' + process.env.MONGO_SERVER + ':' + process.env.MONGO_PORT + '/sails';
mongo.MongoClient.connect(uri, function(err, db) {
if(err){
return res.serverError(err);
}
return res.json("open");
});
{ MongoError: failed to connect to server [192.168.99.2:27027] on first connect [MongoError: connect ETIMEDOUT 192.168.99.2:27027]
at Pool.<anonymous> (....\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> (....\node_modules\mongodb-core\lib\connection\pool.js:280:12)
at Connection.g (events.js:291:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (....\node_modules\mongodb-core\lib\connection\connection.js:187:49)
at Socket.g (events.js:291:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1281:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
name: 'MongoError',
message: 'failed to connect to server [192.168.99.2:27027] on first connect [MongoError: connect ETIMEDOUT 192.168.99.2:27027]' }
I am using the exact same server information used on the datastore.
So what could be wrong here?
And as important: is it possible to use the global Sails connection pool instead of creating a new connection? How? I have searched the documentation but cannot find specific references.
Code was wrong -- using port 27027, while it should be 27017.
Datastore connection worked because of a correct fallback I didn't notice
Try this
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
test.equal(null, err);
test.ok(db != null);
});
Ref : here

Node Is not reading collection

MongoError: failed to connect to server [localhost:27017] on first connect
at .<anonymous> (/Library/WebServer/Documents/nodeprojects/node_modules/mongodb-core/lib/topologies/server.js:313:35)
at emitOne (events.js:96:13)
at emit (events.js:188:7)
at .<anonymous> (/Library/WebServer/Documents/nodeprojects/node_modules/mongodb-core/lib/connection/pool.js:260:12)
at g (events.js:286:16)
at emitTwo (events.js:106:13)
at emit (events.js:191:7)
at Socket.<anonymous> (/Library/WebServer/Documents/nodeprojects/node_modules/mongodb-core/lib/connection/connection.js:162:49)
at Socket.g (events.js:286:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at connectErrorNT (net.js:1015:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
name: 'MongoError',
message: 'failed to connect to server [localhost:27017] on first connect' }
/Library/WebServer/Documents/nodeprojects/node_modules/mongodb/lib/mongo_client.js:225
throw err
^
TypeError: Cannot read property 'collection' of null
at /Library/WebServer/Documents/nodeprojects/server.js:30:29
at connectCallback (/Library/WebServer/Documents/nodeprojects/node_modules/mongodb/lib/mongo_client.js:315:5)
at /Library/WebServer/Documents/nodeprojects/node_modules/mongodb/lib/mongo_client.js:222:11
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)

Resources