Mongo as service error: Cannot determine state of server

herokumongodb

I am trying to use mongodb as a service and started with heroku services ie dashboard.heroku.com/apps/scaleqamongo/resources. I tried multiple addons which they call like mongolab, mongohq. I create databases and then try to add that service address into my app as

db: 'mongodb://username:password@candidate.19.mongolayer.com:10292,candidate.18.mongolayer.com:10292/app25406086',

but it gives me error

Error: Cannot determine state of server
Error: No replica set primary available for query with ReadPreference PRIMARY
    at ReplSet.checkoutReader (/prod/app/dashboard/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/repl_set/repl_set.js:585:14)
    at Cursor.nextObject (/prod/app/dashboard/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:723:48)
    at Cursor.nextObject (/prod/app/dashboard/node_modules/mongoose/node_modules/mongodb/lib/mongodb/scope.js:22:20)
    at Collection.findOne (/prod/app/dashboard/node_modules/mongoose/node_modules/mongodb/lib/mongodb/collection/query.js:157:10)
    at /prod/app/dashboard/node_modules/connect-mongo/lib/connect-mongo.js:214:18
    at MongoStore._get_collection (/prod/app/dashboard/node_modules/connect-mongo/lib/connect-mongo.js:148:21)
    at MongoStore.get (/prod/app/dashboard/node_modules/connect-mongo/lib/connect-mongo.js:213:10)
    at Object.session [as handle] (/prod/app/dashboard/node_modules/express/node_modules/connect/lib/middleware/session.js:311:11)
    at next (/prod/app/dashboard/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.methodOverride [as handle] (/prod/app/dashboard/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:48:5)

While I am able to connect to the database using the shell command

mongo candidate.19.mongolayer.com:10292/app25406086 -u username -p password

What is the problem? Why am I not able to connect?

Best Answer

This is an error with the Mongoose driver/ODM.

Try this (taken from the docs):

var uri = "mongodb://username:password@candidate.19.mongolayer.com:10292,candidate.18.mongolayer.com:10292/app25406086"
var options = {
  replset: { rs_name: 'myReplicaSetName' }
}
mongoose.connect(uri, options);