I am trying to connect to mongoHQ from my node.js app. Here is the code I am using:
var databaseUrl = "mongodb://fishcuss:MyPassword@alex.mongohq.com:10015/app9759558"
var collections = ["users"]
var db = require("mongojs").connect(databaseUrl, collections);
exports.register = function(req, res){
console.log("Register hit")
var user = req.body;
db.users.findOne({username:user.username}, function(err, users) {
console.log(err)
console.log(users);
if( err || users) {
res.statusCode = 500;
res.end();
}
else {
console.log("Inserting new user")
user._id = uuid.v4();
user.lists = [];
db.users.insert(user,{},function(){
req.session.user=user
res.write(JSON.stringify(user), 'UTF-8');
res.statusCode = 200;
res.end();
})
} ;
});
};
However I seem to get this error
{ [MongoError: auth fails] name: 'MongoError', errmsg: 'auth fails', ok: 0 }
Which leads me to believe that I am missing something in my connection. Anyone have a hint as to what that might be?
Thanks
I'm not sure why your code doesn't work but I believe you need to authenticate AFTER opening a connection.
I also use Heroku + MongoLAB, and I use the mongo-db-native driver. That combination seems to work pretty well.
Check out what I do below:
var mongodb = require('mongodb');
mongo_client = new mongodb.Db(mongo_db_name, new mongodb.Server(mongo_url, mongo_port), {safe:true});
mongo_client.open(function(err, p_client) {
if (err)
console.warn("MONGO ERROR:" + err.message);
p_client.authenticate(mongo_db_username,mongo_db_password,{},function(err,success){
if (err) {
console.warn("MONGO ERROR: unauthorized "+ err.message);
} else {
console.log("MONGO Authorized");
mongo_messages_collection = new mongodb.Collection(p_client, 'messages');
}
});
});
Probably, the username/password you are using are not matching. If you are unsure, create a new user on the mongohq console and use those credentials.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.