0

Estoy tratando de reemplazar la base de datos de un repositorio que cloné con mi propia base de datos:

var mongoose = require('mongoose');
var achievementSeeder = require('../app/achievementsSeeder');

// connect to db ===============================================================

(function () {
    "use strict";
    let connection = mongoose.connection,
        connected = false;
    // mongoose.connect(process.env.MONGOOSE_MLAB_URI || 'mongodb://sandboxUser:[email protected]:11880/quizzessandbox'); // former one
    mongoose.connect(process.env.MONGOOSE_MLAB_URI || 'mongodb+srv://sandboxUser:[email protected]/test'); // mine
    connection.once('open', function() {
        console.log("database connection:");
        mongoose.connection.db.dropCollection('achievements', function(err, result) {});
        achievementSeeder.seedAchievement();
        if (process.env.MONGOOSE_MLAB_URI) {
            console.log("sucesfully connected to database");
        } else {
            console.log("sucesfully connected to sandbox database");
        }
    });
    mongoose.connection.on('open', function (ref) {
        connected=true;
        console.log('open connection to mongo server.');
    });

    mongoose.connection.on('connected', function (ref) {
        connected=true;
        console.log('connected to mongo server.');
    });

    mongoose.connection.on('disconnected', function (ref) {
        connected=false;
        console.log('disconnected from mongo server.');
    });

    mongoose.connection.on('close', function (ref) {
        connected=false;
        console.log('close connection to mongo server');
    });

    mongoose.connection.on('error', function (err) {
        connected=false;
        console.log('error connection to mongo server!');
        console.log(err);
    });

    mongoose.connection.db.on('reconnect', function (ref) {
        connected=true;
        console.log('reconnect to mongo server.');
    });
})();

Sin embargo, al ejecutar npm start tengo el siguiente error:

C:\Users\antoi\Documents\Programming\Quizzes\nodejs-quizzes\config\mongoModule.js:48
    mongoose.connection.db.on('reconnect', function (ref) {
                           ^

TypeError: Cannot read property 'on' of undefined
    at C:\Users\antoi\Documents\Programming\Quizzes\nodejs-quizzes\config\mongoModule.js:48:28
    at Object.<anonymous> (C:\Users\antoi\Documents\Programming\Quizzes\nodejs-quizzes\config\mongoModule.js:52:3)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (C:\Users\antoi\Documents\Programming\Quizzes\nodejs-quizzes\index.js:15:1)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\antoi\AppData\Roaming\npm-cache\_logs\2020-05-20T14_16_27_283Z-debug.log

Así que no sé si mi error está relacionado con el Failed at the [email protected] start script. (no sé qué significa) o conmigo y mi base de datos.

1
  • 2
    es mongoose.connection.on sin el db
    – hawks
    Commented el 20 may. 2020 a las 14:24

1 respuesta 1

0

Intenta conectarte a mongo de la siguiente manera


        mongoose.connect('mongodb+srv://sandboxUser:[email protected]/test')
    .then(db =>{
       //Aqui puedes hacer un console.log de correcto
       console.log('Conexion con mongodb exitosa')
    })
    .catch( err => {
      //Aqui va el error al conectar puedes colocar
      console.log('error al conectar con mongodb, ocurrio el siguiente error ' + err)
    }) 

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.