Insertion error: this. _Connection is not a Function

Asked

Viewed 551 times

0

I am trying to make an insertion in the database 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);
}

Usuariosdao.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){
        mongoclient.collection("usuarios", function(err, collection){
            collection.insert(usuario);

            mongoclient.close();
        });
    });
}

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

Does anyone have any idea what it might be?

  • Good evening, which version of Mongodb is installed and which version is on your Nodejs init?

  • Hi. I got it. What I did was just change the version of Mongodb, which was on 3.0.0-rc0, to version 2.2.29.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.