我正在heroku中部署我的应用程序但是我遇到了一些困难。我能够连接到我的本地计算机上的mlab mongo数据库并使用heroku本地网络(虽然由于我没有查看的原因,我无法查询数据库)。以下是来自heroku日志的错误:
017-06-30T03:18:42.576989+00:00 app[web.1]: /app/node_modules/mongodb/lib/mongo_client.js:377
2017-06-30T03:18:42.576990+00:00 app[web.1]: throw err
2017-06-30T03:18:42.576991+00:00 app[web.1]: ^
2017-06-30T03:18:42.576996+00:00 app[web.1]: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
2017-06-30T03:18:42.576997+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:328:35)
2017-06-30T03:18:42.576998+00:00 app[web.1]: at emitOne (events.js:115:13)
2017-06-30T03:18:42.576999+00:00 app[web.1]: at Pool.emit (events.js:210:7)
2017-06-30T03:18:42.576999+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:280:12)
2017-06-30T03:18:42.577000+00:00 app[web.1]: at Object.onceWrapper (events.js:318:30)
2017-06-30T03:18:42.577001+00:00 app[web.1]: at emitTwo (events.js:125:13)
2017-06-30T03:18:42.577001+00:00 app[web.1]: at Connection.emit (events.js:213:7)
2017-06-30T03:18:42.577002+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:177:49)
2017-06-30T03:18:42.577003+00:00 app[web.1]: at Object.onceWrapper (events.js:316:30)
2017-06-30T03:18:42.577004+00:00 app[web.1]: at emitOne (events.js:115:13)
2017-06-30T03:18:42.577004+00:00 app[web.1]: at Socket.emit (events.js:210:7)
2017-06-30T03:18:42.577005+00:00 app[web.1]: at emitErrorNT (internal/streams/destroy.js:62:8)
2017-06-30T03:18:42.577006+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:102:11)
2017-06-30T03:18:42.577007+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:161:9)
2017-06-30T03:18:42.677447+00:00 heroku[web.1]: State changed from starting to crashed
2017-06-30T03:18:42.671887+00:00 heroku[web.1]: Process exited with status 1
我的代码如下(我已删除了一些内容,只是为了尝试并保持简短):
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var mongoose = require('mongoose');
var mongoStore = require('connect-mongo/es5')({ session: expressSession });
require('./models/users_model.js');
mongoose.Promise = global.Promise;
var MONGOLAB_URI = "mongodb://username:password@ etc.";
var connection = mongoose.connect(MONGOLAB_URI, {useMongoClient: true},function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', MONGOLAB_URI);
}
});