I'm trying to make an insert into the bank using nodejs and mongodb, but I get the following error:
TypeError: this._connection.open is not a function
at UsuariosDAO.inserirUsuario (E:\docs\projetos\task_manager\app\models\UsuariosDAO.js:12:19)
at Object.module.exports.cadastrar (E:\docs\projetos\task_manager\app\controllers\cadastroController.js:24:14)
at E:\docs\projetos\task_manager\app\routes\cadastroRoute.js:8:50
at Layer.handle [as handle_request] (E:\docs\projetos\task_manager\node_modules\express\lib\router\layer.js:95:5)
at next (E:\docs\projetos\task_manager\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (E:\docs\projetos\task_manager\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (E:\docs\projetos\task_manager\node_modules\express\lib\router\layer.js:95:5)
at E:\docs\projetos\task_manager\node_modules\express\lib\router\index.js:281:22
at Function.process_params (E:\docs\projetos\task_manager\node_modules\express\lib\router\index.js:335:12)
at next (E:\docs\projetos\task_manager\node_modules\express\lib\router\index.js:275:10)
Here are the codes ...
dbConnection.js:
var mongo = require('mongodb');
/* Wrapper */
var conMongoDB = function(){
var db = new mongo.Db(
'taskmanager',
new mongo.Server(
'localhost', //string contendo o endereço do servidor
27017, //porta de conexão
{}
),
{}
);
return db;
}
/* Função exportada para executar no autoload */
module.exports = function () {
return conMongoDB;
}
cadastroController.js:
module.exports.cadastro = function(application, req, res){
res.render('cadastro', {validacao: {}, dados: {}});
}
module.exports.cadastrar = function(application, req, res){
var dados = req.body;
req.assert('usuario', 'O campo usuario não pode ser vazio').notEmpty();
req.assert('senha', 'O campo senha não pode ser vazio').notEmpty();
var erros = req.validationErrors();
if(erros){
res.render('cadastro', {validacao: erros, dados: dados});
return;
}
var connection = application.config.dbConnection;
var UsuariosDAO = new application.app.models.UsuariosDAO(connection);
UsuariosDAO.inserirUsuario(dados);
}
UsuariosDA.js
function UsuariosDAO(connection){
this._connection = connection;
}
UsuariosDAO.prototype.inserirUsuario = function(usuario){
/* Abrir conexão com mongo */
console.log(this._connection);
this._connection.open( function(err, mongoclient){
//através do mongoclient é que manipulamos as collection e documentos
mongoclient.collection("usuarios", function(err, collection){
collection.insert(usuario);
mongoclient.close();
});
});
}
module.exports = function(){
return UsuariosDAO;
}
Does anyone have any idea what it can be?