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
Related
I am facing an issue using node.js and mongoose, when I am trying to run the following query I get this error. I tried almost every possible solution but nothing helped out.
my mongo DB version is 5.0.6, node.js is v14.17.1 and the mongoose version is ^6.0.10
const getConversationsToJoin = async(userId) => {
const conversations = await Conversation.find({ status: 'active' }, {
$or: [{
member_a_id: userId
},
{
member_b_id: userId
}
]
});
return conversations
}
(node:23468) UnhandledPromiseRejectionWarning: MongoServerError: FieldPath field names may not start with '$'. Consider using $getField or
$setField.
at MessageStream.messageHandler (D:\laragon\www\Tuxedo_API\node_modules\mongodb\lib\cmap\connection.js:467:30)
at MessageStream.emit (events.js:375:28)
at processIncomingData (D:\laragon\www\Tuxedo_API\node_modules\mongodb\lib\cmap\message_stream.js:108:16)
at MessageStream._write (D:\laragon\www\Tuxedo_API\node_modules\mongodb\lib\cmap\message_stream.js:28:9)
at writeOrBuffer (internal/streams/writable.js:358:12)
at MessageStream.Writable.write (internal/streams/writable.js:303:10)
at Socket.ondata (internal/streams/readable.js:726:22)
at Socket.emit (events.js:375:28)
at addChunk (internal/streams/readable.js:290:12)
at readableAddChunk (internal/streams/readable.js:265:9)
(Use node --trace-warnings ... to show where the warning was created)
(node:23468) 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:23468) [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.
Please help
Your issue is nothing related to the version of technologies used, it's an incorrect use of find query. Find query will only take one argument, that is supposed to contain all your filters.
Moreover the syntax for $or is incorrect too. It should not be wrapped inside curly braces.
Here is what you should be doing:
const getConversationsToJoin = async (userId) => {
const conversations = await Conversation.find({
status: "active",
$or: [
{
member_a_id: userId,
},
{
member_b_id: userId,
},
],
});
return conversations;
};
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!
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)
I am trying to use windows authentication using the package https://www.npmjs.com/package/sequelize-msnodesqlv8 but I am getting SequelizeConnectionError.
This is my configuration.
{
dialect: "mssql",
dialectModulePath: "sequelize-msnodesqlv8",
dialectOptions: {
driver: "SQL Server Native Client 11.0",
trustedConnection: true
},
host: process.env.DEV_DB_HOSTNAME,
database: process.env.DEV_DB_NAME
}
The error that I am getting:
(node:27588) UnhandledPromiseRejectionWarning: SequelizeConnectionError
at Connection.connection.on.err (C:\webportaltesttool\backend\node_modules\sequelize\lib\dialects\mssql\connection-manager.js:85:18)
at Connection.emit (events.js:182:13)
at mssql.open (C:\webportaltesttool\backend\node_modules\sequelize-msnodesqlv8\lib\connection.js:107:9)
at Immediate._onImmediate (C:\webportaltesttool\backend\node_modules\msnodesqlv8\lib\connection.js:360:15)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
(node:27588) 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 hand
led with .catch(). (rejection id: 1)
(node:27588) [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:27588) UnhandledPromiseRejectionWarning: SequelizeConnectionError
at Connection.connection.on.err (C:\webportaltesttool\backend\node_modules\sequelize\lib\dialects\mssql\connection-manager.js:85:18)
at Connection.emit (events.js:182:13)
at mssql.open (C:\webportaltesttool\backend\node_modules\sequelize-msnodesqlv8\lib\connection.js:107:9)
at Immediate._onImmediate (C:\webportaltesttool\backend\node_modules\msnodesqlv8\lib\connection.js:360:15)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
(node:27588) 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 hand
led with .catch(). (rejection id: 2)
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