Node Is not reading collection - node.js

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)

Related

MongoNetworkError node.js

Server up and running on port 5000 !
{ MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: Authentication failed.]
at Pool.<anonymous> (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/topologies/server.js:431:11)
at Pool.emit (events.js:198:13)
at connect (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/pool.js:580:14)
at callback (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connect.js:109:5)
at provider.auth.err (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connect.js:352:21)
at _authenticateSingleConnection (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/auth/auth_provider.js:66:11)
at sendAuthCommand (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/auth/scram.js:177:16)
at Connection.messageHandler (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connect.js:334:5)
at Connection.emit (events.js:198:13)
at processMessage (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connection.js:364:10)
at Socket.<anonymous> (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connection.js:533:15)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
As the error indicates, please use the correct username and password
make sure that mongodb server is running
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database connected");
});
MongoError: Authentication failed is the error for invalid credentials.please use your username and password for secure connection.
mongoose.connect('mongodb://username:password#host:port/database');

MongoNetworkError: connection 5 to ercluster-shard-00-01-8xhj2.mongodb.net:27017 timed out

I am using react and nodejs.When i send query to mongodb , after some time (2-3 min) i am geting this error(MongoNetworkError timed out). I am using cluster that i created on mongo atlas.
I tried to create new account and cluster on mongo atlas , set up new mongo uri ,but the problem is same.
console.log("mongoUri:", config.mongoUri);
const options = {
useNewUrlParser: true
// keepAlive: 300000,
// connectTimeoutMS: 30000
};
mongoose.connect(config.mongoUri, options, err => {
console.log("error occured", err);
});
console.log("mongoose connection:", mongoose.connection.readyState);
mongoose.connection.readyState;
error occured { MongoNetworkError: connection 4 to etcluster-shard-00-01-u7zep.mongodb.net:27017 timed
out
at TLSSocket.<anonymous> (C:\Users\Petar\Documents\GitHub\eliteranks\elite-ranks-back\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:335:7)
at Object.onceWrapper (events.js:273:13)
at TLSSocket.emit (events.js:182:13)
at TLSSocket.Socket._onTimeout (net.js:449:8)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
events.js:167
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:111:27)
Emitted 'error' event at:
at TLSSocket.<anonymous> (C:\Users\Petar\Documents\GitHub\eliteranks\elite-ranks-back\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:321:10)
at Object.onceWrapper (events.js:273:13)
at TLSSocket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
[nodemon] app crashed - waiting for file changes before starting...

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.

NetworkingError AWS SES

I am sending emails using AWS SES. It was working fine, but recently I am getting Networking errors. Full stack track is as followling.
Error: write EPROTO 140495130142528:error:140770FC:SSL
routines:SSL23_GET_SERVER_HELLO:unknown
protocol:../deps/openssl/openssl/ssl/s23_clnt.c:827:
at _errnoException (util.js:1022:11)
at WriteWrap.afterWrite [as oncomplete] (net.js:880:14)
message: 'write EPROTO 140495130142528:error:140770FC:SSL
routines:SSL23_GET_SERVER_HELLO:unknown
protocol:../deps/openssl/openssl/ssl/s23_clnt.c:827:\n',
code: 'NetworkingError',
errno: 'EPROTO',
syscall: 'write',
region: 'us-east-1',
hostname: 'email.us-east-1.amazonaws.com',
retryable: true,
time: 2018-07-26T09:57:44.096Z
I am also getting following error.
TimeoutError: Connection timed out after 120000ms
at ClientRequest.<anonymous> (/var/runtime/node_modules/aws-
sdk/lib/http/node.js:83:34)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at ClientRequest.emit (events.js:208:7)
at TLSSocket.emitTimeout (_http_client.js:711:34)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at TLSSocket.emit (events.js:208:7)
at TLSSocket.Socket._onTimeout (net.js:420:8)
at ontimeout (timers.js:482:11)
message: 'Connection timed out after 120000ms',
code: 'TimeoutError',
time: 2018-07-26T10:09:31.149Z,
region: 'us-east-1',
hostname: 'email.us-east-1.amazonaws.com',
retryable: true
How to resolve this issue?

mongodb connection timeout when callback function after query is slow

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?

Resources