Reputation: 455
I'm working on an Angular 7 app with MongoDB, Node and Express. If I start my Express app (using npm start command) before connecting to MongoDB (with mongod command), the Express app first throws an error because it's unable to establish connection with MongoDB. Once MongoDB is up and running, the Express app informs me that MongoDB is now connected at port 27017. However, any http post requests I execute through my Angular app cause Express to return a 200 status code (which tells me everything is ok), but MongoDB fails to create a document as a result of the http post request. How do I make sure that MongoDB is not only connected but that the connection can successfully create the new document when I execute the post http request? I read somewhere that MongoDB's ability to save/create a document requires it to have an open connection. In that regard, what's the difference between having an open connection and MongoDB being connected at port 27017?
Here's the code I use in my Express app.js file to connect to MongoDB:
var express = require('express');
var mongoose = require('mongoose');
var app = express();
var mongoose_uri = process.env.MONGOOSE_URI || "mongodb://abc:abc123@localhost:27017/databank?authSource=admin";
mongoose.set('debug', true);
mongoose.connect(mongoose_uri);
mongoose.connection.on('connected', ()=>{
console.log('MongoDB connected at port 27017');
});
//Not sure if the mongoose.connection.once is necessary to have, considering I already have mongoose.connection.on above.
mongoose.connection.once('open', ()=>{
console.log('MongoDB connection now open');
})
//MongoDB connection error
mongoose.connection.on('error', (err)=>{
console.log(err);
})
Here's the npm log, showing the connection error at first, followed by successful connection, followed by several Post requests with status code 200, yet nothing got saved to the MongoDB collection.
[nodemon] 1.19.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./bin/www`
API Gateway listening at http://localhost:8085/api
Web Server listening at http://localhost:8085/
{ Error: connect ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
name: 'MongoError',
message: 'connect ECONNREFUSED 127.0.0.1:27017' }
MongoDB connected at port 27017
POST /api/contactus 200 335.509 ms - 18
POST /api/contactus 200 9.082 ms - 18
POST /api/contactus 200 3.916 ms - 18
POST /api/contactus 200 6.268 ms - 18
POST /api/contactus 200 61.876 ms - 18
Of course, this problem was resolved when I restarted my express app after an active mongoDB session, but I won't always have the luxury of inspecting logs and the app's ability to create documents when in production. Appreciate some guidance.
Upvotes: 0
Views: 1764
Reputation: 1276
You have to connect to mongo first, then initialize express.
mongoose.connection.on('connected', ()=>{
console.log('MongoDB connected at port 27017');
app = express();
});
//once open event is not necessary
After that, you can consider writing init functions that all return promises. Like that you can chain it and all is clear. Here is an example where rabbit, then mongo, then express is inited.
initRabbit()
.then(initMongo)
.then(initExpress)
.catch(e => {
error({error:"boot", cause: e})
process.exit(-1)
})
const initMongo = () => new Promise(resolve => mongoose.connection.on('connected', resolve))
Upvotes: 1