我正在尝试使用 mongodb 作为服务,并从 heroku 服务(即仪表盘.heroku.com/apps/scaleqamongo/resources)开始。我尝试了多个插件,它们被称为 mongolab、mongohq。我创建数据库,然后尝试将该服务地址添加到我的应用程序中
db: 'mongodb://username:[email protected]:10292,candidate.18.mongolayer.com:10292/app25406086',
但它给了我错误
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)
虽然我可以使用 shell 命令连接到数据库
mongo candidate.19.mongolayer.com:10292/app25406086 -u username -p password
有什么问题?为什么我无法连接?
答案1
这是 Mongoose 驱动程序/ODM 的错误。
尝试这个(取自文档):
var uri = "mongodb://username:[email protected]:10292,candidate.18.mongolayer.com:10292/app25406086"
var options = {
replset: { rs_name: 'myReplicaSetName' }
}
mongoose.connect(uri, options);