I have a two server. One have replica set enabled and other is just for store the backups. Now I want to make my api connect to secondary server. And for that I am following the Approach which was as follow:
mongoose.connection.close();
mongoose.connect('mongodb://127.0.0.1:27021/secondary', { useMongoClient: true });
Message.find({}).limit(2).exec(function(error, data) {
if(error) {
console.log();
} else {
res.send(data);
mongoose.connection.close();
mongoose.connect('mongodb://127.0.0.1:27017, 127.0.0.1:27018, 127.0.0.1:27020/uio_new?replicaSet=uio', { useMongoClient: true });
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
}
});
By this approach I am getting the response from second server but just after sending the response it returns error i.e
MongoError: topology was destroyed
Any one have any Idea why thses things happen. Please suggest some best approach. When these API hits at that time only I want the response form second server. Else Second server is not in use. Any help is really appreciated.