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.
Related
I'm trying to access my localserver, whenever I attempt to start the site and login, it crashes and I get a few different errors that I've posted below.
MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
name: 'MongoNetworkError',
[Symbol(mongoErrorContextSymbol)]: {}
}]
at Pool.<anonymous> (/Users/user/dev/P2/frontpage/node_modules/mongodb/lib/core/topologies/server.js:438:11)
at Pool.emit (events.js:311:20)
at /Users/user/dev/P2/frontpage/node_modules/mongodb/lib/core/connection/pool.js:561:14
at /Users/user/dev/P2/frontpage/node_modules/mongodb/lib/core/connection/pool.js:994:11
at /Users/user/dev/P2/frontpage/node_modules/mongodb/lib/core/connection/connect.js:31:7
at callback (/Users/user/dev/P2/frontpage/node_modules/mongodb/lib/core/connection/connect.js:264:5)
at Socket.<anonymous> (/Users/user/dev/P2/frontpage/node_modules/mongodb/lib/core/connection/connect.js:294:7)
at Object.onceWrapper (events.js:418:26)
at Socket.emit (events.js:311:20)
at emitErrorNT (internal/streams/destroy.js:92:8) {
name: 'MongoNetworkError',
[Symbol(mongoErrorContextSymbol)]: {}
}
I've tried re-installing Mongo but with no luck.
Thanks
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');
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...
I am not able to solve the mongodb connection problem. I am getting the error below on starting node app.
Here is my mongoose connection code.
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/ml').then(
() => { console.log('Connected to mongodb!') },
err => { console.log('Failed to connect to mongodb'); console.log(err); }
);
module.exports = {mongoose};
Here is the error.
Started up at port 80
Failed to connect to mongodb
{ MongoNetworkError: failed to connect to server [10.160.0.2:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 10.160.0.2:27017]
at Pool.<anonymous> (/home/shivamsharma_btp2/mlsever/magguland/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at Pool.emit (events.js:189:13)
at Connection.<anonymous> (/home/shivamsharma_btp2/mlsever/magguland/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:277:13)
at Connection.emit (events.js:189:13)
at Socket.<anonymous> (/home/shivamsharma_btp2/mlsever/magguland/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:277:13)
at Socket.emit (events.js:189: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)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Here are firewall rules of my google cloud app.
Got solution!
Use sudo before the command to start server.
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