I'm trying to configure the connetion in my dbConnection.js
to connect my MongoDb hosted in Mlab to my NodeJs app hosted in Heroku.
I believe the problem is permisson.
My question: How can I configure the "connectionString" using ONLY the default MongoDb Driver (without Mongoose and similars for exemple plz. I want to make and learn with basic and default method)
My apllication has a App.Js that calls server.js
/* importar as configurações do servidor */
var app = require('./config/server');
var port = (process.env.PORT || 8080);
/* parametrizar a porta de escuta */
app.listen(port, function(){
console.log('');
console.log('|*****************************************************|');
console.log('|----------- Servidor online na porta: ' + port + ' ----------|');
console.log('|*****************************************************|');
console.log('');
})
My server.js uses MongoDb including dbConnection.js.
server.js
...some code
mongodb = require('mongodb'), // <--- var para o banco de dados
objectId = require('mongodb').ObjectId;
//var url = PROD_MONGODB //'mongodb://localhost:27017/my_database_name';
var url = process.env.MONGOLAB_URI;
... some code
My dbConnection.js var connMongoDB is:
/* Importar o MongoDB */
var mongo = require('mongodb');
/*var connMongoDB = function(){
console.log('========== Entrou na function de connection! =============');
var db = new mongo.Db(
'OPDC', // PARAM 1 = nome do banco
new mongo.Server( // PARAM 2 = objetos basicos de conexao
'localhost', // string contendo o endereco do servidor
27017, // porta
{} // opc de config do servidor
),
{} // PARAM 3 = configs opcionais
);
return db;
}*/
var connMongoDB = function(){
console.log('========== Entrou na function de connection! =============');
var db = new mongo.Db(
'heroku_f6bst99r', // PARAM 1 = database name
new mongo.Server( // PARAM 2 = basic objects for connection
'ds129776.mlab.com', // string with the server address
29776, // port
{} // server config options?
),
{} // PARAM 3 = opcionals configs
);
return db;
}
module.exports = function(){
return connMongoDB;
}
Some observations:
- Localhost (app and database) all works
- Published Node still works without database connection (a simple return message for exemple
- I can Connect and use my hosted Mlab MongoDb still
The error is that below: Address: https://opdcapi.herokuapp.com/ Error:
{"name":"MongoError","message":"not authorized on heroku_f6bst99r to execute command { find: \"brand\", filter: {} }","ok":0,"errmsg":"not authorized on heroku_f6bst99r to execute command { find: \"brand\", filter: {} }","code":13,"codeName":"Unauthorized"}
GITHUB PROJECT: https://github.com/frotaadriano/opdc_nodeAPI