Boa noite, usando essa variável de ambiente tive erro.
DB_CONNECTION_STRING = mongodb+srv://pedroismael:senha@cluster0.sgzxyua.mongodb.net/livraria?retryWrites=true&w=majority
Obs, no lugar da senha coloquei senha por segurança.
-------------------------------------------------------------------------------------------------------------------------------------------------
PS C:\Users\verde\OneDrive\Área de Trabalho\Estudo\ALURA\API Rest node.js> npm run dev
> api-rest-node@1.0.0 dev
> nodemon server.js
[nodemon] 3.0.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node server.js`
erro de conexão 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 NativeConnection.createClient (C:\Users\verde\OneDrive\Área de Trabalho\Estudo\ALURA\API Rest node.js\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:206:11)
at NativeConnection.openUri (C:\Users\verde\OneDrive\Área de Trabalho\Estudo\ALURA\API Rest node.js\node_modules\mongoose\lib\connection.js:779:34)
at Mongoose.connect (C:\Users\verde\OneDrive\Área de Trabalho\Estudo\ALURA\API Rest node.js\node_modules\mongoose\lib\mongoose.js:404:15)
at conectaNaDataBase (file:///C:/Users/verde/OneDrive/%C3%81rea%20de%20Trabalho/Estudo/ALURA/API%20Rest%20node.js/src/config/bdConnect.js:5:14)
at file:///C:/Users/verde/OneDrive/%C3%81rea%20de%20Trabalho/Estudo/ALURA/API%20Rest%20node.js/src/app.js:5:23
at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
at async loadESM (node:internal/process/esm_loader:34:7)
at async handleMainPromise (node:internal/modules/run_main:113:12)
Servidor escutando
node:internal/process/promises:289
triggerUncaughtException(err, true /* fromPromise */);
^
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 NativeConnection.createClient (C:\Users\verde\OneDrive\Área de Trabalho\Estudo\ALURA\API Rest node.js\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:206:11)
at NativeConnection.openUri (C:\Users\verde\OneDrive\Área de Trabalho\Estudo\ALURA\API Rest node.js\node_modules\mongoose\lib\connection.js:779:34)
at Mongoose.connect (C:\Users\verde\OneDrive\Área de Trabalho\Estudo\ALURA\API Rest node.js\node_modules\mongoose\lib\mongoose.js:404:15)
at conectaNaDataBase (file:///C:/Users/verde/OneDrive/%C3%81rea%20de%20Trabalho/Estudo/ALURA/API%20Rest%20node.js/src/config/bdConnect.js:5:14)
at file:///C:/Users/verde/OneDrive/%C3%81rea%20de%20Trabalho/Estudo/ALURA/API%20Rest%20node.js/src/app.js:5:23
at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
at async loadESM (node:internal/process/esm_loader:34:7)
at async handleMainPromise (node:internal/modules/run_main:113:12)
Node.js v20.10.0
[nodemon] app crashed - waiting for file changes before starting...
-----------------------------------------------------------------------------------------------------------------------------------------------
Troque o nome da variável para MONGODB_URI e funcionou!!!!! Qual a diferença para essa DB_CONNECTION_STRING?