web-dev-qa-db-fra.com

mongoose.connect (), le premier argument doit être String, reçu non défini

J'essaie de définir la base de données de test à des fins de test, mais cela ne fonctionne pas.

J'essaie de me connecter à mongodb en utilisant mangouste, mais la recherche d'un problème d'erreur de connexion indique:

throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
    ^
MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()`is a string.
    at new MongooseError (/media/abhigyan/ABHI/programming/node js/Practice/On my Own/Todo/node_modules/mongoose/lib/error/mongooseError.js:11:11)
    at NativeConnection.Connection.openUri (/media/abhigyan/ABHI/programming/node js/Practice/On my Own/Todo/node_modules/mongoose/lib/connection.js:424:11)
    at Mongoose.connect (/media/abhigyan/ABHI/programming/node js/Practice/On my Own/Todo/node_modules/mongoose/lib/index.js:230:15)
    at Object.<anonymous> (/media/abhigyan/ABHI/programming/node js/Practice/On my Own/Todo/server/db/mongoose.js:5:10)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/media/abhigyan/ABHI/programming/node js/Practice/On my Own/Todo/server/models/Todo.js:1:82)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/media/abhigyan/ABHI/programming/node js/Practice/On my Own/Todo/server/tests/server.test.js:4:16)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at /media/abhigyan/ABHI/programming/node js/Practice/On my Own/Todo/node_modules/mocha/lib/mocha.js:250:27
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/media/abhigyan/ABHI/programming/node js/Practice/On my Own/Todo/node_modules/mocha/lib/mocha.js:247:14)
    at Mocha.run (/media/abhigyan/ABHI/programming/node js/Practice/On my Own/Todo/node_modules/mocha/lib/mocha.js:576:10)
    at Object.<anonymous> (/media/abhigyan/ABHI/programming/node js/Practice/On my Own/Todo/node_modules/mocha/bin/_mocha:637:18)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
error Command failed with exit code 1.

Je passe une chaîne valide, mais ça ne marche pas!

const mongoose = require('mongoose');


mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI, err => {
    if(err) 
        console.log(err);
    } 
);



module.exports = {
    mongoose
};

Voici le script pour exécuter le moka:

export NODE_ENV='test' && mocha server/**/*.test.js

Voici le code de configuration:

const config = require('./config.json');

const env = process.env.NODE_ENV.toString() || 'development';

if(env === 'test' || env === 'development') {
    const envConfig = config[env];
    Object.keys(envConfig).forEach(key => {
        process.env[key] = envConfig[key];
    });
};

console.log(env);

Voici le fichier config.json: 

{
    "test": {
        "PORT": 3000,
        "MONGODB_URI": "mongodb://localhost:27017/TodoTest"
    },
    "development": {
        "PORT": 3000,
        "MONGODB_URI": "mongodb://localhost:27017/Todo"
    }
}

Merci pour l'aide!

2
abhigyan nayak

Je rencontrais aussi le même problème après la méthode add code{ useNewUrlParser: true }in mongoose.connect (). Problème résolu.

mongoose.connect(config.DB,{ useNewUrlParser: true }));
1
Anjyr

Je pense que vous manquez l'importation du fichier env.

require('dotenv').config({ path: 'ENV_FILENAME' });
1
simba

Assurez-vous que vous avez le fichier .env avec les constantes que vous utilisez définies.

0
Kenaa

C'est ce qui a résolu mon problème. Bon codage!

// Connect to MongoDB
    mongoose.connect('mongodb://yourusername:[email protected]:11025/yourmongodb', {useNewUrlParser: true});
    mongoose.connection.once('open', function(){
      console.log('Conection has been made!');
    }).on('error', function(error){
        console.log('Error is: ', error);
    });
0
Andrew Polemeni

J'ai eu un problème similaire, résolu en ajoutant cet extrait de code. 

mongoose.connect(config.DB,{ useMongoClient:true });
0
Sikandar Khan