Reputation: 3
If anyone can understand my issue,I went through many posts but I didn't found anything which is helpful
This is my index.js file
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
require("dotenv").config();
//Import Routes
const authRoute = require("./routes/auth");
//connnect to DB
mongoose.connect(process.env.DB_CONNECT, { useNewUrlParser: true }, () =>
console.log("Connected To DB!")
);
//Middlewares
app.use(express.json());
//route Middleware
app.use("/api/user", authRoute);
app.listen(3000, () => console.log("server up and Running"));
This is .env file
DB_CONNECT = 'mongodb+srv://username:<password>@cluster0.5tnd3.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'
PORT = 3000
This is the error I am getting in my terminal
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 NativeConnection.Connection.openUri (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\connection.js:694:11)
at C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\index.js:350:10
at promiseOrCallback (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\helpers\promiseOrCallback.js:9:12)
at Mongoose._promiseOrCallback (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\index.js:1154:10)
at Mongoose.connect (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\index.js:349:20)
at Object.<anonymous> (C:\COURSES\Authentication-nodeJS\index.js:12:10)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...
(node:4364) UnhandledPromiseRejectionWarning: 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.Connection.openUri (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\connection.js:694:11)
at C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\index.js:350:10
at C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
at Mongoose._promiseOrCallback (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\index.js:1154:10)
at Mongoose.connect (C:\COURSES\Authentication-nodeJS\node_modules\mongoose\lib\index.js:349:20)
at Object.<anonymous> (C:\COURSES\Authentication-nodeJS\index.js:13:4)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:4364) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:4364) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
And Now I am getting this weird error.but its showing that DB is connected.Can anyone please help me
Thank you in advance
Upvotes: 0
Views: 1543
Reputation: 71
server up and Running Successfully and DB Connected
(node:11416) UnhandledPromiseRejectionWarning: MongoError: Authentication failed.
at MessageStream.messageHandler (C:\COURSES\Authentication-nodeJS\node_modules\mongodb\lib\cmap\connection.js:268:20)
at MessageStream.emit (events.js:315:20)
at processIncomingData (C:\COURSES\Authentication-nodeJS\node_modules\mongodb\lib\cmap\message_stream.js:144:12)
at MessageStream._write (C:\COURSES\Authentication-nodeJS\node_modules\mongodb\lib\cmap\message_stream.js:42:5)
at writeOrBuffer (internal/streams/writable.js:358:12)
at MessageStream.Writable.write (internal/streams/writable.js:303:10)
at TLSSocket.ondata (internal/streams/readable.js:719:22)
at TLSSocket.emit (events.js:315:20)
at addChunk (internal/streams/readable.js:309:12)
at readableAddChunk (internal/streams/readable.js:284:9)
at TLSSocket.Readable.push (internal/streams/readable.js:223:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11416) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:11416) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
emphasized textyou can follow the following steps, I think your code should work after this
://username:<password>
DB_CONNECT = 'mongodb+srv://username:<password>@cluster0.5tnd3.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'
to something like this
MONGO_URL_TEST= "mongodb+srv://myname:[email protected]/test?retryWrites=true&w=majority"
const options = {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
};
mongoose.connect(MONGO_URL, options);
const dotenv = require("dotenv");
below is enough
require('dotenv').config();
4.make sure your .env file is at the root of your file not inside any folder
read more here:
https://mongoosejs.com/docs/connections.html
https://mongoosejs.com/docs/deprecations.html
Server is Running But Still Getting These Errors and Warnings. Should I really worry about this? Because I am getting empty data in postman if I verify
Upvotes: 1