Mongoose无法在本地连接到Heroku数据库并且无法连接到process.env.MONGOLAB_URI

时间:2015-03-23 19:58:24

标签: node.js mongodb heroku mongoose mlab

我正按照书中的说明操作。我在终端连接到本地数据库或MongoLab在线时没有问题,但无法使用我的Heroku数据库在本地运行我的应用程序:NODE_ENV=production nodemon bin/www

这是我在我的代码中检查使用在线数据库,具体取决于我如何启动应用程序:

if (process.env.NODE_ENV === 'production') {
    dbURI= process.env.MONGOLAB_URI;
}

我手动输入:heroku config:set MONGOLAB_URI=[my uri]并使用heroku config:get MONGOLAB_URI并继续获取:Error: failed to connect to [undefined:27017]

有没有人知道为什么我无法连接?

1 个答案:

答案 0 :(得分:0)

~ $ node app.js 

/app/node_modules/connect-mongo/node_modules/mongodb/lib/server.js:228
        process.nextTick(function() { throw err; })
                                            ^
Error
    at Error.MongoError (/app/node_modules/connect-mongo/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:13:17)
    at Server.destroy (/app/node_modules/connect-mongo/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:629:47)
    at Server.close (/app/node_modules/connect-mongo/node_modules/mongodb/lib/server.js:344:17)
    at Db.close (/app/node_modules/connect-mongo/node_modules/mongodb/lib/db.js:267:19)
    at /app/node_modules/connect-mongo/node_modules/mongodb/lib/db.js:196:12
    at null.<anonymous> (/app/node_modules/connect-mongo/node_modules/mongodb/lib/server.js:226:9)
    at g (events.js:180:16)
    at emit (events.js:98:17)
    at null.<anonymous> (/app/node_modules/connect-mongo/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:238:68)
    at g (events.js:180:16)
    at emit (events.js:98:17)
    at null.<anonymous> (/app/node_modules/connect-mongo/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:77:12)
    at g (events.js:180:16)
    at emit (events.js:98:17)
    at Socket.<anonymous> (/app/node_modules/connect-mongo/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:118:49)
    at Socket.g (events.js:180:16)
~ $ 

^^我看到了以上情况 我的联系:

omeMongodbServer:{     适配器:&#39; sails-mongo&#39;,     url:process.env.MONGOLAB_URI || &#39; mongodb的://本地主机/ TS&#39 ;,   },