我无法连接到MongoLAB中的MongoDB。我有一个MEAN
应用程序,我已部署在heroku中。当我尝试连接数据库但不能。日志如下所示。
Disconnected from log stream. There may be events happening that you do not see here! Attempting to reconnect...
2017-07-31T11:46:48.243603+00:00 app[web.1]: at module.exports (/app/node_modules/mongodb/lib/url_parser.js:59:13)
2017-07-31T11:46:48.243605+00:00 app[web.1]: at Function.MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:113:3)
2017-07-31T11:46:48.243604+00:00 app[web.1]: at connect (/app/node_modules/mongodb/lib/mongo_client.js:289:16)
2017-07-31T11:46:48.243606+00:00 app[web.1]: at run (/app/node_modules/thunky/index.js:13:3)
2017-07-31T11:46:48.243606+00:00 app[web.1]: at /app/node_modules/mongojs/lib/database.js:29:15
2017-07-31T11:46:48.243607+00:00 app[web.1]: at Collection._getConnection (/app/node_modules/thunky/index.js:27:3)
2017-07-31T11:46:48.243608+00:00 app[web.1]: at Collection._getCollection (/app/node_modules/mongojs/lib/collection.js:17:10)
2017-07-31T11:46:48.243609+00:00 app[web.1]: at getCursor (/app/node_modules/mongojs/lib/collection.js:32:10)
2017-07-31T11:46:48.243610+00:00 app[web.1]: at run (/app/node_modules/thunky/index.js:13:3)
2017-07-31T11:46:48.243609+00:00 app[web.1]: at /app/node_modules/mongojs/lib/cursor.js:12:5
2017-07-31T11:46:48.244240+00:00 app[web.1]: [0mPOST /login [31m500 [0m30.134 ms - 22[0m
Disconnected from log stream. There may be events happening that you do not see here! Attempting to reconnect...
我正在解释下面的代码。
var mongoJs=require('mongojs');
var CryptoJS = require("crypto-js");
var database='FGDP';
var collections=['f_users'];
var MONGOLAB_URI="mongodb://username:password@ds127153.mlab.com:27153/fgdp";
var db=mongoJs(MONGOLAB_URI, collections);
var ses;
exports.userlogin=function(req,res){
var username=req.body.user_name;
var password=req.body.user_pass;
// console.log('pass'+typeof(password));
//var ciphertext = CryptoJS.AES.encrypt(password, 'lexelPass');
//var pass=ciphertext.toString();
db.f_users.findOne({'login_name':username},function(err,docs){
console.log('err',err);
if(!err){
if(docs){
var pass=docs.password;
var bytes = CryptoJS.AES.decrypt(pass.toString(), 'lexelPass');
var plaintext = bytes.toString(CryptoJS.enc.Utf8);
if(plaintext==password){
req.session.email=docs.email;
req.session.name=docs.name;
req.session.login_name=docs.login_name;
req.session.status=docs.status;
req.session.user_type=docs.user_type;
res.send(docs);
}else{
res.send("Login failed");
}
}
}
if(err){
res.send("Login failed");
}
})
}
该数据库和集合存在于mongoLAB中。我需要通过mongojs连接它。实际上我有一个登录应用程序,我无法登录。
答案 0 :(得分:0)
你应该这样做
var db = mongoJs(MONGOLAB_URI);
db.on('connect', function () {
console.log('database connected')
});