UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string - node.js

when i am running my app.js file i am getting UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string.
the whole error in my terminal is:
$ node app.js
(node:18676) UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string
at parseConnectionString (C:\Users\Dell\Documents\Extra\fruit\node_modules\mongodb\lib\core\uri_parser.js:565:21)
at connect (C:\Users\Dell\Documents\Extra\fruit\node_modules\mongodb\lib\operations\connect.js:282:3)
at C:\Users\Dell\Documents\Extra\fruit\node_modules\mongodb\lib\mongo_client.js:224:5
at maybePromise (C:\Users\Dell\Documents\Extra\fruit\node_modules\mongodb\lib\utils.js:665:3)
at MongoClient.connect (C:\Users\Dell\Documents\Extra\fruit\node_modules\mongodb\lib\mongo_client.js:220:10)
at C:\Users\Dell\Documents\Extra\fruit\node_modules\mongoose\lib\connection.js:820:12
at new Promise ()
at NativeConnection.Connection.openUri (C:\Users\Dell\Documents\Extra\fruit\node_modules\mongoose\lib\connection.js:817:19)
at C:\Users\Dell\Documents\Extra\fruit\node_modules\mongoose\lib\index.js:345:10
at C:\Users\Dell\Documents\Extra\fruit\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
at new Promise ()
at promiseOrCallback (C:\Users\Dell\Documents\Extra\fruit\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
at Mongoose._promiseOrCallback (C:\Users\Dell\Documents\Extra\fruit\node_modules\mongoose\lib\index.js:1135:10)
at Mongoose.connect (C:\Users\Dell\Documents\Extra\fruit\node_modules\mongoose\lib\index.js:344:20)
at Object. (C:\Users\Dell\Documents\Extra\fruit\app.js:3:10)
at Module._compile (internal/modules/cjs/loader.js:1076:30)
(Use node --trace-warnings ... to show where the warning was created)
(node:18676) 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: 3)
(node:18676) [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.
(node:18676) UnhandledPromiseRejectionWarning: MongooseError: Operation fruits.insertOne() buffering timed out after 10000ms
at Timeout. (C:\Users\Dell\Documents\Extra\fruit\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:184:20)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
(node:18676) 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: 4)
The Code is given below:
const mongoose=require('mongoose');
mongoose.connect('monogodb://localhost:27017/fruitsDB',{ useNewUrlParser:true,family:4});
const fruitSchema=new mongoose.Schema({
name:String,
rating:Number,
review:String
});
const Fruit=mongoose.model("Fruit",fruitSchema);
const fruit=new Fruit({
name:"Apple",
rating:7,
review:"Pretty Good!"
});
fruit.save();

You have a typo at the beginning of your connection string, monogodb instead of mongodb.

Solution make sure your MongoDB URL connection string is correct or misspelled
"mongodb://myDBReader:D1fficultP%40ssw0rd#mongodb0.example.com:27017/?authSource=admin" or
"mongodb://mongodb0.example.com:27017"

Related

(node:1928) UnhandledPromiseRejectionWarning: MongooseError: Operation `users.createIndex()` buffering timed out

Getting the following error when I tried to estalish connection to my MongoDB Atlas.
[nodemon] starting `node index.js`
Note-It backend listening at http://localhost:5000
(node:1928) UnhandledPromiseRejectionWarning: MongooseError: Operation `users.createIndex()` buffering timed out after 10000ms
at Timeout.<anonymous> (C:\Users\Meet\OneDrive\Desktop\note-it-backend-api\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:149:23)
at listOnTimeout (internal/timers.js:557:17)
at processTimers (internal/timers.js:500:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:1928) 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:1928) [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.
***********************Connected to MongoDB successfully***********************
I cant make any HTTP requests because of this
The db.js contents are :
const mongoose = require('mongoose');
// Connection string for MongoDB ATLAS
const mongoURL = "mongodb+srv://new-meeti:<password>#meet-dev.cc0qw.mongodb.net/?retryWrites=true&w=majority"
const connectToMongo = () => {
// .connect method taks 2 args, 2nd one is a callback function
mongoose.connect(mongoURL, () => {
console.log("***********************Connected to MongoDB successfully***********************");
})
}
module.exports = connectToMongo;
This was more than a year old project hosted on heroku but then I had to shift to to some other hosting service so again deploying is getting problematic due to various deprecation issues I think.
A help would be really appreciated. Thanks :)
PS : I checked my Network access on the MongoDB Atlas and 0.0.0.0/0 (includes your current IP address) is active.

I've tried connecting my mongoose to my express app, using the following, it'd then connect at first and then state error later on

Code:
mongoose.connect('mongodb://localhost:27017/blog'), {
useUnifiedTopology: true,
useNewUrlParser: true,
}
console.log('MongoDB connected successfully!!');
output:
MongoDB connected successfully!!
(node:2868) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at NativeConnection.Connection.openUri (C:\Users\v\Desktop\News Blog\node_modules\mongoose\lib\connection.js:824:32)
at C:\Users\v\Desktop\News Blog\node_modules\mongoose\lib\index.js:381:10
at C:\Users\v\Desktop\News Blog\node_modules\mongoose\lib\helpers\promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Users\v\Desktop\News Blog\node_modules\mongoose\lib\helpers\promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (C:\Users\v\Desktop\News Blog\node_modules\mongoose\lib\index.js:1234:10)
at Mongoose.connect (C:\Users\v\Desktop\News Blog\node_modules\mongoose\lib\index.js:380:20)
at Object.<anonymous> (C:\Users\v\Desktop\News Blog\server.js:7:10)
at Module._compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2868) 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:2868) [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.

Error: connect ECONNREFUSED 127.0.0.1:27017

I am having this error popped up whenever I try to connect to my mongo Server from mongoose.
I have also tried using different port numbers and deleting the DB file and recreating it. But still the same error log
terminal output
UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at NativeConnection.Connection.openUri (/home/Desktop/CRUDJS/node_modules/mongoose/lib/connection.js:797:32)
at /home/Desktop/CRUDJS/node_modules/mongoose/lib/index.js:332:10
at /home/Desktop/CRUDJS/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (/home/Desktop/CRUDJS/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/home/Desktop/CRUDJS/node_modules/mongoose/lib/index.js:1153:10)
at Mongoose.connect (/home/Desktop/CRUDJS/node_modules/mongoose/lib/index.js:331:20)
at Object.<anonymous> (/home/Desktop/CRUDJS/app.js:7:10)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
(node:44838) 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:44838) [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.

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'findIndex' of undefined..... IN MY NODEJS Project

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'findIndex' of undefined
at User.addToCart (D:\Pradip_All\Nodejs- tutorials video (MONGODB)\models\user.js:20:46)
at D:\Pradip_All\Nodejs- tutorials video (MONGODB)\controllers\shop.js:70:23
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11212) 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:11212) [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.
ERROR DESCRIPTION
You should add the ? optional chaining operator that check if the value exists in the object.
const cartProductIndex = this.cart?.items?.findIndex((cp) => {
return cp.productId.toString() === product._id.toString()
})

Google Sheets node.js api gives an error "Unhandled promise rejection" when attempting to output changed spreadsheet data to console

function checkForChanges() {
let spreadsheetId = '1oHNELBZ3zV4PQBVsJrtqX4Cz00IFmJG_2SaJ4bgTa3c'
let ranges = ['A2:P2', 'A3:P3', 'A4:P4', 'A5:P5']
this.batchGet ({
spreadsheetId,
ranges,
}, (err, result) => {
if (err) {
console.log(err);
} else {
console.log(`${result.valueRanges.length} ranges retrieved.`);
}
})
}
client.on('ready', () => {
while (true) {
checkForChanges();
}
});
I'm expecting that when I change something on the spreadsheet that it returns in console with what was changed. but instead I get this error when i run the program:
[nodemon] starting `node --inspect turnip_bot.js`
Debugger listening on ws://127.0.0.1:9229/b52c6ec8-70cb-451a-9b70-fc8bada920fc
For help, see: https://nodejs.org/en/docs/inspector
(node:1200) UnhandledPromiseRejectionWarning: TypeError: this.batchGet is not a function
at checkForChanges (D:\Programing\Node.js\Discord-Turnip-Bot\turnip_bot.js:72:7)
at Client.<anonymous> (D:\Programing\Node.js\Discord-Turnip-Bot\turnip_bot.js:87:3)
at Client.emit (events.js:323:22)
at WebSocketManager.triggerClientReady (D:\Programing\Node.js\Discord-Turnip-Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:433:17)
at WebSocketManager.checkShardsReady (D:\Programing\Node.js\Discord-Turnip-Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:417:10)
at WebSocketShard.<anonymous> (D:\Programing\Node.js\Discord-Turnip-Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:199:14)
at WebSocketShard.emit (events.js:311:20)
at WebSocketShard.checkReady (D:\Programing\Node.js\Discord-Turnip-Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:466:12)
at WebSocketShard.onPacket (D:\Programing\Node.js\Discord-Turnip-Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:438:16)
at WebSocketShard.onMessage (D:\Programing\Node.js\Discord-Turnip-Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
(node:1200) 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:1200) [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.
I took the code off of the google sheets API quickstart and edited it slightly for my use case and used some of the documentation to help with reading the spreadsheet on mutiple rows.
If I run the sample it returns:
[nodemon] starting `node --inspect google_sheets_api_sample.js`
Debugger listening on ws://127.0.0.1:9229/04ef1aed-3c10-40e5-b8f2-78782b5537bc
For help, see: https://nodejs.org/en/docs/inspector
(node:3208) UnhandledPromiseRejectionWarning: Error: authentication failed
at authorize (D:\Programing\Node.js\Discord-Turnip-Bot\google_sheets_api_sample.js:57:11)
at main (D:\Programing\Node.js\Discord-Turnip-Bot\google_sheets_api_sample.js:13:28)
at Object.<anonymous> (D:\Programing\Node.js\Discord-Turnip-Bot\google_sheets_api_sample.js:42:1)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47
(node:3208) 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:3208) [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.
[nodemon] clean exit - waiting for changes before restart

Resources