I need to connect to the Mongo database using the mLO addOn. But I'm not able to insert or list, when running in local mode is working. I have the variable set:
Anyideawhatitmightbe?
Myapp.js
varexpress=require('express');varpath=require('path');varfavicon=require('serve-favicon');varlogger=require('morgan');varcookieParser=require('cookie-parser');varbodyParser=require('body-parser');varroutes=require('./routes/index');varusers=require('./routes/users');varmongoose=require('mongoose');varSchema=mongoose.Schema;varapp=express();//Herewefindanappropriatedatabasetoconnectto,defaultingto//localhostifwedon'tfindone.//'mongodb://heroku_28mj3757:[email protected]:31597/heroku_28mj3757';//MONGODB_URIvaruristring=process.env.MONGOLAB_URI||process.env.MONGOHQ_URL||'mongodb://localhost/HelloMongoose';////viewenginesetupapp.set('views',path.join(__dirname,'views'));app.set('viewengine','jade');//uncommentafterplacingyourfaviconin/public//app.use(favicon(path.join(__dirname,'public','favicon.ico')));app.use(logger('dev'));app.use(bodyParser.json());app.use(bodyParser.urlencoded({extended:false}));app.use(cookieParser());app.use(express.static(path.join(__dirname,'public')));app.use('/',routes);app.use('/users',users);//catch404andforwardtoerrorhandlerapp.use(function(req,res,next){varerr=newError('NotFound');err.status=404;next(err);});//errorhandlers//developmenterrorhandler//willprintstacktraceif(app.get('env')==='development'){app.use(function(err,req,res,next){res.status(err.status||500);res.render('error',{message:err.message,error:err});});}//productionerrorhandler//nostacktracesleakedtouserapp.use(function(err,req,res,next){res.status(err.status||500);res.render('error',{message:err.message,error:{}});});//Thehttpserverwilllistentoanappropriateport,ordefaultto//port5000.console.log("O valor da porta é: " +process.env.PORT);
console.log("O valor da porta é: " +uristring);
var theport = process.env.PORT || 5000;
// Makes connection asynchronously. Mongoose will queue up database
// operations and release them when the connection is complete.
mongoose.connect(uristring, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + uristring + '. ' + err);
} else {
console.log ('Succeeded connected to: ' + uristring);
}
});
module.exports = app;
Routes where I insert / list;
var userSchema = new mongoose.Schema({
name: String,
age: Number
});
var PUser = mongoose.model('PowerUsers', userSchema);
router.post('/insert', function(req, res, next) {
//res.send({nome:"nome"});
//console.("Ola mundo");
//var Inscricaoo = mongoose.model('pessoas', inscricao);
console.log("Route - passou aqui ");
// Creating one user.
var johndoe = new PUser ({
"name":"sara",
"age": 25
});
// Saving it to the database.
johndoe.save(function (err, result) {
console.log("Errroooooo" + err);
});
res.send(johndoe);
});
router.get('/userlist', function(req, res, next) {
//res.send({nome:"nome"});
//console.("Ola mundo");
//var Inscricaoo = mongoose.model('pessoas', inscricao);
console.log("Route user list- passou aqui ");
// list all .
PUser.find({}, function(err, users) {
var userMap = {};
users.forEach(function(user) {
userMap[user._id] = user;
});
res.send(userMap);
});
});
heroku logs: