Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoose.connect(), first argument should be String, received undefined

I am trying to set the test database for the testing purpose, but its not working.

I am trying to connect to MongoDB using mongoose, but finding problem in the connection error shows:

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. 

I am passing a valid String, but Its not working!

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

Here is the Script to run mocha:

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

Here is the configuration code:

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); 

Here is the config.json file:

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

Thanks for the help!

like image 651
abhigyan nayak Avatar asked Aug 09 '18 15:08

abhigyan nayak


2 Answers

I think you miss importing env file.

require('dotenv').config({ path: 'ENV_FILENAME' }); 
like image 55
Oni Avatar answered Sep 28 '22 19:09

Oni


To read from .env file you have to install dotenv ( npm i dotenv / yarn add dotenv) and then just add this on top of your file.

 const dotenv = require("dotenv");  dotenv.config(); 
like image 35
ASHISH R Avatar answered Sep 28 '22 19:09

ASHISH R