bug in connection to cluster mongodb - node.js

i'm using nodejs to connect to mongodb and i get this error
i added the IP and nothing happend
(node:7412) UnhandledPromiseRejectionWarning: MongoNetworkError: connection 5 to cluster0-shard-00-02-xobf0.mongodb.net:27017 closed
at TLSSocket.<anonymous> (C:\Users\user\Desktop\NodeJS - The Complete Guide\node_modules\mongodb-core\lib\connection\connection.js:352:9)
at Object.onceWrapper (events.js:277:13)
at TLSSocket.emit (events.js:189:13)
at _handle.close (net.js:597:12)
at TCP.done (_tls_wrap.js:388:7)
(node:7412) 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:7412) [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

Try this ,
const MongoClient = require("mongodb").MongoClient;
const uri ="mongodb+srv://user:userpassword#meanapp-srlw9.mongodb.net/test?retryWrites=true";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
console.log("connencted");
client.close();
});
For now, change your cluster setting to connect from anywhere

Related

UnhandledPromiseRejectionWarning: MongoParseError: option autoreconnect is not supported

index.js file
import mongoose from "mongoose";
const DB_CONNECTION_URL =
"mongodb+srv://user:<password>#cluster0.cjkjw.mongodb.net/WhatsAppDB?retryWrites=true&w=majority";
const connectDB = () => {
console.log("DB trying to connect on " + new Date());
const options = {
keepAlive: 1,
maxPoolSize: 10,
useNewUrlParser: true,
autoReconnect: true,
useUnifiedTopology: true,
};
return mongoose.connect(DB_CONNECTION_URL, options);
};
export default connectDB;
eroor in my console
> whatsapp#1.0.0 start C:\Users\krish\onedrive\desktop\whatsapp_clone-mern_Stack\whatsapp-clone\server
> node ./node_modules/babel-cli/bin/babel-node.js --presets node8 ./server.js
DB trying to connect on Tue Nov 30 2021 21:31:43 GMT+0530 (India Standard Time)
(node:2944) UnhandledPromiseRejectionWarning: MongoParseError: option autoreconnect is not supported
at parseOptions (C:\Users\krish\onedrive\desktop\whatsapp_clone-mern_Stack\whatsapp-clone\server\node_modules\mongodb\src\connection_string.ts:358:11)
at new MongoClient (C:\Users\krish\onedrive\desktop\whatsapp_clone-mern_Stack\whatsapp-clone\server\node_modules\mongodb\src\mongo_client.ts:327:34)
at C:\Users\krish\onedrive\desktop\whatsapp_clone-mern_Stack\whatsapp-clone\server\node_modules\mongoose\lib\connection.js:779:16
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (C:\Users\krish\onedrive\desktop\whatsapp_clone-mern_Stack\whatsapp-clone\server\node_modules\mongoose\lib\connection.js:776:19)
at C:\Users\krish\onedrive\desktop\whatsapp_clone-mern_Stack\whatsapp-clone\server\node_modules\mongoose\lib\index.js:332:10
at C:\Users\krish\onedrive\desktop\whatsapp_clone-mern_Stack\whatsapp-clone\server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Users\krish\onedrive\desktop\whatsapp_clone-mern_Stack\whatsapp-clone\server\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (C:\Users\krish\onedrive\desktop\whatsapp_clone-mern_Stack\whatsapp-clone\server\node_modules\mongoose\lib\index.js:1153:10)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2944) 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:2944) [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 need to connect to my localhost when i run this it shows the above error. i used poolsize instead of maxPoolsize and it shows the same error showing poolsize is not supported but when searched for the solution it shows there is some updation for the version and the new version doesnt support poolsize so i changed to maxpoolsize and it works but i didnt found any answer for the autorecconet i hope there is also an update for autoReconnet in the new version . but when i remove the auto reconnet code it works!! please help me to fix this!

How to connect to replicaset using mongoose?

I am trying to connect to mongodb replica sets from mongoose following https://mongoosejs.com/docs/connections.html#replicaset_connections
I was able to connect using below syntax in #1 but it throws the errorUnhandledPromiseRejectionWarning: MongoError: not master and slaveOk=false
I looked at other posts with similar error and tried #2 and #3 but the database connection fails altogether,can some provide guidance on how to connect to replica sets without throwing the below error?
#1
mongoose
.connect(
"mongodb://username:password#replicaset1:replicaset1port/myDB", { useNewUrlParser: true }
)
#2:
mongoose
.connect(
"mongodb://username:password#replicaset1:replicaset1port,replicaset2:replicaset2port,replicaset3:replicaset3port/myDB?replicaSet=rsName", { useNewUrlParser: true }
)
#3
mongoose
.connect(
"mongodb://username:password#replicaset1:replicaset1port,replicaset2:replicaset2port,replicaset3:replicaset3port/myDB", { useNewUrlParser: true }
)
Error:-
(node:8) UnhandledPromiseRejectionWarning: MongoError: not master and slaveOk=false
at Connection.<anonymous> (/backend/node_modules/mongodb-core/lib/connection/pool.js:443:61)
at Connection.emit (events.js:315:20)
at processMessage (/backend/node_modules/mongodb-core/lib/connection/connection.js:364:10)
at Socket.<anonymous> (/backend/node_modules/mongodb-core/lib/connection/connection.js:533:15)
at Socket.emit (events.js:315:20)
at addChunk (internal/streams/readable.js:309:12)
at readableAddChunk (internal/streams/readable.js:284:9)
at Socket.Readable.push (internal/streams/readable.js:223:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
(node:8) 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:8) [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.
undefined

Error Connecting Google Sheets to Node JS

I'm trying to have a spreadsheet interact with node js and I'm having connectivity issues. I don't quite know where I'm going wrong and I can't seem to find a remedy to my situation. My code is below and it is a mix of Twilio and what I've found here:
const GoogleSpreadsheet = require('google-spreadsheet');
const creds = require('./credentials.json');
// spreadsheet key is the long id in the sheets URL
async function accessSpreadsheet() {
const doc = new GoogleSpreadsheet('XXX');
await doc.useServiceAccountAuth({
client_email: creds.client_email,
private_key: creds.private_key,
});
await doc.loadInfo(); // loads document properties and worksheets
console.log(doc.title);
const sheet = doc.sheetsByIndex[0]; // or use doc.sheetsById[id]
console.log(sheet.title);
console.log(sheet.rowCount);
}
accessSpreadsheet();
I'm new to JavaScript so I don't really know how to properly handle these errors. My error messages are below:
(node:15432) UnhandledPromiseRejectionWarning: TypeError: doc.loadInfo is not a function
at accessSpreadsheet (/home/spreadsheet.js:13:13)
(node:15432) 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:15432) [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.
/home/index.js:77
cb()
^
TypeError: cb is not a function
at /home/google-spreadsheet/index.js:77:7
at /home/node_modules/google-auth-library/lib/auth/jwtclient.js:119:5
at /home/node_modules/google-auth-library/lib/auth/jwtclient.js:138:16
at Request._callback (/home/node_modules/gtoken/lib/index.js:228:14)
at Request.self.callback (/home/node_modules/request/request.js:185:22)
at Request.emit (events.js:203:13)
at Request.<anonymous> (/home/node_modules/request/request.js:1154:10)
at Request.emit (events.js:203:13)
at IncomingMessage.<anonymous> (/home/node_modules/request/request.js:1076:12)
at Object.onceWrapper (events.js:291:20)

Chrome launcher exits with UnhandledPromiseRejectionWarning

I am trying to set up chrome-launcher to output all console messages to terminal. My code looks like this
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
(async function() {
async function launchChrome() {
return await chromeLauncher.launch({
chromeFlags: [
'--window-size=1200,800',
'--user-data-dir=/tmp/chrome-testing',
'--auto-open-devtools-for-tabs'
]
});
}
const chrome = await launchChrome();
const protocol = await CDP({
port: chrome.port
});
const {
DOM,
Network,
Page,
Runtime,
Console
} = protocol;
await Promise.all([Network.enable(), Page.enable(), DOM.enable(), Runtime.enable(), Console.enable()]).catch(console.log);
// REMARKS: messageAdded is fired every time a new console message is added
Console.messageAdded((result) => {
console.log(result);
});
})();
I copied some of this from the question here: How to get console.log output in Terminal via Headless Chrome Runtime.evaluate
When I try to navigate to a page, none of the console messages show up in the terminal, and the chrome-launcher exits with the following error:
(node:14531) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:64656
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1083:14)
(node:14531) 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:14531) [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.

Why am I getting this error with selenium chromedriver?

When the code below is executed it opens chrome and the search bar is filled already with this data;,. I haven't mentioned it anywhere in the code still this happens.
Moreover, there are few errors that appear each time I try to run the code.
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var driver =new webdriver.Builder()
.forBrowser('chrome')
.build();
driver.get('http://www.google.com/ncr');
driver.findElement(By.name('q')).sendKeys('wiki');
driver.findElement(By.name('btnG')).click();
driver.wait(check_title,1000);
function check_title(){
var promise = driver.getTitle().then((title)=>{
if(title === 'wiki - Google Search'){
console.log('success!');
return true;
}else{
console.log("failed!!");
}
});
return promise;
}
I get this error
(node:15384) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:61227
(node:15384) [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:15384) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:61227
(node:15384) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:61227
(node:15384) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 5): Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:61227
(node:15384) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 6): Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:61227
How can this be removed?
Use .clear(); to clear the input field for the search bar before you send keys to it. For details on what it does see the answer to this question.
The Promise is not handling the reject with a catch. Following the answers from another question on promise rejection.
var promise = driver.getTitle().then((title)=>{
if(title === 'wiki - Google Search') {
console.log('success!');
return true;
} else {
console.log("failed!!");
}
}).catch(function () {
console.log("Promise Rejected!");
});

Resources