Problems connecting Node and MongoDb

0

Some days ago I'm having a problem with a code that I can not resolve. What I am trying to do at the moment is to register the users in mongodb through the node. I am using the view mechanism ejs, but until then I have checked everything and it is correct, the only problem is in the implementation of the function that opens the connection with the Mongodb bank. I'm using the MVC standard, below the codes.

dbConnection.js file that connects to the database (so far everything seems ok):

/* importar o mongodb */
var mongo = require('mongodb');

var connMongoDB = function(){
    console.log('Entrou na função de conexão');
    var db = new mongo.Db(
        'crud',
        new mongo.Server('localhost',
            27017,
            {}
        ),
        {}
    );

    return db;
};

module.exports = function(){
    return connMongoDB;
};

Route cadastro.js (seems to be ok tbm):

module.exports = function(application, res, res){
application.get('/cadastro', function(req, res){
    application.app.controllers.cadastro.cadastro(application, req, res);
});

application.post('/cadastrar', function(req, res){
    application.app.controllers.cadastro.cadastrar(application, req, res);
});
};

Controller registration (Until then I still did the test passing a console.log from the variable DataForm and was returning the data typed in the form normally):

module.exports.cadastro = function(application, req, res){
res.render('cadastro', {validacao: {}, dadosForm: {}});
};

module.exports.cadastrar = function(application, req, res){

var dadosForm = req.body;

req.assert('nome', 'Nome não pode ser vazio').notEmpty();
req.assert('usuario', 'Usuário não pode ser vazio').notEmpty();
req.assert('senha', 'Senha não pode ser vazio').notEmpty();

var erros = req.validationErrors();

if(erros){
    res.render('cadastro', {validacao: erros, dadosForm: dadosForm});
    return;
};

var connection = application.config.dbConnection; // retorna a variável connMongoDb
var UsuariosDAO = new application.app.models.UsuariosDAO(connection); // passa a variável connMongoDb acima como parâmetro

UsuariosDAO.inserirUsuario(dadosForm);

res.send('Cadastrado com sucesso!');
};

UsersDOS File. The error begins at line 6 of code:

function UsuariosDAO(connection){
this._connection = connection(); // recebe a variável db do arquivo dbConnection
};

UsuariosDAO.prototype.inserirUsuario = function(usuario){
this._connection.open( function(err, mongoclient){
    mongoclient.collection("users", function(err, collection){
       collection.insert(usuario);


    });
});
};

module.exports = function(){
return UsuariosDAO;
};

For better visualization of the problem, it follows error code:

TypeError: this._connection.open is not a function
at UsuariosDAO.inserirUsuario (c:\Users\micha\GitHub\Projetos-Praticos\Projeto-CRUD\app\models\UsuariosDAO.js:6:22)
at Object.module.exports.cadastrar (c:\Users\micha\GitHub\Projetos-Praticos\Projeto-CRUD\app\controllers\cadastro.js:23:17)
at c:\Users\micha\GitHub\Projetos-Praticos\Projeto-CRUD\app\routes\cadastro:7:46
at Layer.handle [as handle_request] (c:\Users\micha\GitHub\Projetos-Praticos\Projeto-CRUD\node_modules\express\lib\router\layer.js:95:5)
at next (c:\Users\micha\GitHub\Projetos-Praticos\Projeto-CRUD\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (c:\Users\micha\GitHub\Projetos-Praticos\Projeto-CRUD\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (c:\Users\micha\GitHub\Projetos-Praticos\Projeto-CRUD\node_modules\express\lib\router\layer.js:95:5)
at c:\Users\micha\GitHub\Projetos-Praticos\Projeto-CRUD\node_modules\express\lib\router\index.js:281:22
at Function.process_params (c:\Users\micha\GitHub\Projetos-Praticos\Projeto-CRUD\node_modules\express\lib\router\index.js:335:12)
at next (c:\Users\micha\GitHub\Projetos-Praticos\Projeto-CRUD\node_modules\express\lib\router\index.js:275:10)

I hope I've been able to expose the problem correctly. I will be very grateful to you!

    
asked by anonymous 23.09.2018 / 03:15

0 answers