While I try to connect mongodb with express js with mongoose and I am getting error for a long. How to fix this is ?
const express = require('express');
const mongoose = require('mongoose');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from node js')
})
const connection_url = 'connection-url';
mongoose.connect(connection_url, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(()=> console.log("Database Connected"));
app.listen(3000);
This is the error
node:14460) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
at NativeConnection.Connection.openUri (C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\connection.js:846:32)
at C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\index.js:351:10
at C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\index.js:1149:10)
at Mongoose.connect (C:\MyFolder\Coding\DB connection\node_modules\mongoose\lib\index.js:350:20)
at Object.<anonymous> (C:\MyFolder\Coding\DB connection\index.js:11:10)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:14460) 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:14460) [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.
How to fix this is and connect to the mongodb ??
Go to MongoDB Atlas
Open your cluster
Go to settings > Network Access
Add IP Addresss
Click on Allow access on anywhere || Just add your current IP
Restart your app and it should be working
Related
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.
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.
When I have done everything correctly and have scripted correctly the comes up when i put node . in command prompt I even have a valid token!! ::
(node:8744) UnhandledPromiseRejectionWarning: Error [TOKEN_INVALID]: An invalid token was provided. at Client.login (C:\Users\User\Documents\GitHub TigerDuo\TigerDuo\node_modules\discord.js\src\client\Client.js:204:52) at Object.<anonymous> (C:\Users\User\Documents\GitHub TigerDuo\TigerDuo\index.js:47:5) at Module._compile (internal/modules/cjs/loader.js:1133:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10) at Module.load (internal/modules/cjs/loader.js:977:32) at Function.Module._load (internal/modules/cjs/loader.js:877:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47 (node:8744) 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:8744) [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 this is my script let alone i have setup heroku and github:
const Discord = require('discord.js');
const bot = new Discord.Client();
const token = 'MyNeverToBeSharedToken';
bot.on('ready', () => {
console.log('Online.')
bot.user.setActivity('the Tiger Duo server!', {
type: "WATCHING"
})
bot.login(process.env.token);
Try installing dotenv package
require("dotenv").config()
And add this to the top of your code
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
I want to use transactions with mongoose, therefore I installed run-rs to test it in the development stage. According to guides like this one https://medium.com/cashpositive/the-hitchhikers-guide-to-mongodb-transactions-with-mongoose-5bf8a6e22033 run-rs starts it on the localhost. When I run run-rs -v 4.2.5 --keep --shell to start the replica-set via the terminal it says: Started replica set on "mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs". Why is it saying DESKTOP for me instead of localhost and how would I connect to it in mongoose.connect()?
I tried the following in my index.js:
const app = express();
const PORT = 27017;
const dbUri = 'mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs'
mongoose.Promise = global.Promise;
mongoose.connect(dbUri, {
replicaSet: 'rs',
useNewUrlParser: true,
useUnifiedTopology: true
});
Also how would I append the databasename?
UPDATE:
PS C:\Users\Workstation\project> npm start
> project#1.0.0 start C:\Users\Workstation\project
> nodemon ./index.js --exec babel-node -e js
[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js
[nodemon] starting `babel-node ./index.js`
Your server is running on Port 4000
(node:7436) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27018
at new MongooseServerSelectionError (C:\Users\Workstation\project\node_modules\mongoose\lib\error\serverSelection.js:22:11)
at NativeConnection.Connection.openUri (C:\Users\Workstation\ project\node_modules\mongoose\lib\connection.js:823:32)
at Mongoose.connect (C:\Users\Workstation\ project\node_modules\mongoose\lib\index.js:333:15)
at Object.<anonymous> (C:/Users/Workstation/project/index.js:22:10)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at loader (C:\Users\Workstation\project\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.<computed> [as .js] (C:\Users\Workstation\project\node_modules\babel-register\lib\node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
at Object.<anonymous> (C:\Users\Workstation\project\node_modules\babel-cli\lib\_babel-node.js:154:22)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
(node:7436) 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(). (rejection id: 1)
(node:7436) [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.
UPDATE
I accepted the answer below, because the string for connecting works, hence the original question is solved. However I switched to Linux and it works now without further setup. run-rs didn't work on my Windows machine it seems.
replace:
const dbUri = 'mongodb://DESKTOP-U9IDO6F:27017,DESKTOP-U9IDO6F:27018,DESKTOP-U9IDO6F:27019?replicaSet=rs'
with:
const dbUri = 'mongodb://localhost:27017,localhost,localhost:27019/databasenaeme'
Also need to run mongoDb at these port manully. use link for refrence:
link