Unable to connect to my database from my node js application - javascript

I've been trying to connect my application to my database, but it has not been possible. I've tried this below, and it still did not recognize my models
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = process.env.MONGODB_URL;
// Database Name
const dbName = 'KaydeeAcedemy';
// Use connect method to connect to the server
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = client.db(dbName);
client.close();
});
I've also tried using this connection string, yet no headway.
const { MongoClient } = require('mongodb');
const uri = process.env.MONGODB_URL;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
when I try to add a new user from my application, i get this error as a result of the database.
Operation `users.findOne()` buffering timed out after 10000ms
the mongodb url looks like this:
MONGODB_URL = mongodb+srv://Kaydeeacademy:**************#cluster0.ehclf.mongodb.net/KaydeeAcademy?retryWrites=true&w=majority
I don't know if I missed something in the connection string or somewhere.

Can you test/compare out some of my code (working)
// have you called in the dot.env file?
require('dotenv').config()
const express = require('express')
const mongoose = require('mongoose')
// don't include the "useNewUrlParser: true, useUnifiedTopology: true" see answer below:
// https://stackoverflow.com/questions/68915722/option-usefindandmodify-is-not-supported
// make sure there are no spaces in the dot.env file
mongoose.connect(process.env.MONGO_URI)
.then(() => console.log("db connected!"))
.catch(err => console.error("db connection failed ", err))

Related

MongoDB client in Node

Im trying to connect mongodb in node.js and take the link to client to the outer variable.
I need to create a module that would return a result of .find() method. How can i do this?
const MongoClient = require('mongodb').MongoClient
let client
mongoClient = new MongoClient('mongodb://localhost:27017/', {useUnifiedTopology: true, useNewUrlParser: true})
mongoClient.connect((err, db) => {
if (err) {
return console.log(err)
}
client = db
})
const database = client.db('db')
const collection = database.collection('collection')
client.close()
And the error
const database = client.db('db')
^
TypeError: Cannot read property 'db' of undefined
As the comment suggests, you can use async/await to wait for the connection to be established, and do the error handling with a try/catch statement:
try {
const client = await mongoClient.connect()
const database = client.db('db')
const collection = database.collection('collection')
} catch(e) {
console.log(e)
}
Don't forget to use it in an async function.
Update
An async function example, you may want to return the collection:
const getData = async () => {
try {
mongoClient = new MongoClient('mongodb://localhost:27017/', {useUnifiedTopology: true, useNewUrlParser: true})
const client = await mongoClient.connect()
const database = client.db('db')
const collection = database.collection('collection')
client.close()
return collection
} catch(e) {
console.log(e)
}
}
after uri you must define db name
mongoClient = new MongoClient('mongodb://localhost:27017/', {useUnifiedTopology: true, useNewUrlParser: true})
like this
mongoClient = new MongoClient('mongodb://localhost:27017/shop', {useUnifiedTopology: true, useNewUrlParser: true})

How to separate the mongoose connect method to another JS file?

For my discord bot, I was thinking of separating the mongoose connect method and other initialization to a separate JS file. I was able to move everything except for the connect method. I don't know how this could be done.
Try below code
// Declare mongoose as global
global.mongoose = require('mongoose');
// In Separate file
const url = "Your URL";
//BUILD A CONNECTION
mongoose.connect(url).then(() => { console.log('Connected To database :)')})
.catch( err => console.log('error', err));
module.exports.mongoose = mongoose
// Schema File
const users = new mongoose.Schema({
name : String,
}, {
timestamps : true
})
module.exports = mongoose.model('users', users);
// In model Path
const usersModel = require('Schemas/users')
now you can use usersModel to query documents/table
// suppose this filename is, mongoose.db.config.js
const mongoose = require("mongoose");
function mongooseConnectDB(uri) {
mongoose
.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
})
.then((result) =>
console.log("Mongoose connected to ", result.connections[0].host)
)
.catch((err) => console.log("error connecting to the database", err));
}
module.exports = mongooseConnectDB;
Now in your app(main.js/index.js/server.js), you can do simply like this
const mongooseConnectDB = require("./mongoose.db.config");
mongooseConnectDB('url_of_your_db');

View Mongo DB in Mongo compass

I'm new to MongoDB and Node. I have connected to Mongo using below
const mongoose = require("mongoose")
mongoose.connect('mongodb://localhost/testaroo', {
useNewUrlParser: true
})
mongoose.connection.once('open', function() {
console.log("Connection has been made ..")
}).on('error', function(error) {
console.log("Connection error", error)
});
And I created the below model
const mongoose = require('mongoose')
const schema = mongoose.Schema;
const marioCharSchema = new schema({
name: String, // Optional
weight: Number // Optional
})
const marioCharModel = mongoose.model('mariochar', marioCharSchema);
module.exports = marioCharModel;
EDIT
Inserting Data
const assert = require('assert');
const mariochar = require('../test/models/mariochar')
// Describe tests
describe('Saving records', function () {
//Create tests
it("Saving records to database", function (done) {
const char = new mariochar({
name = "Test",
weight: 45
});
char.save().then(
function () {
assert(char.isNew === false)
done();
}
)
})
})
And I tried to save a new record to this model and I tested it using mocha and it saved successfully. Now I want to view my database and my records using Mongo compass.
I tried to connect to mongodb://localhost/testaroo on port 27017
but I couldn't see my database. So what I'm missing here?
FIXED
For future viewers I fixed it by passing this option useUnifiedTopology: true to the to the MongoClient constructor
mongoose.connect('mongodb://localhost/Mydb', { useNewUrlParser: true, useUnifiedTopology: true })
Use mongo shell to insert and retrieve documents from the database, verify this works.
Use compass to view documents you inserted via the shell, verify this works.
Insert documents in your application and retrieve them via the shell, verify this works.
At this point whatever issue you have between your application and compass should be resolved.

TypeError: Cannot read property 'db' of undefined while trying to Mongodb Atlas Online

[nodemon] starting node server.js
C:\Users\Abhay\Desktop\todo-app\node_modules\mongodb\lib\utils.js:725
throw error;
^
TypeError: Cannot read property 'db' of undefined
at C:\Users\Abhay\Desktop\todo-app\server.js:8:17
at C:\Users\Abhay\Desktop\todo-app\node_modules\mongodb\lib\utils.js:722:9
at C:\Users\Abhay\Desktop\todo-app\node_modules\mongodb\lib\mongo_client.js:223:23
at C:\Users\Abhay\Desktop\todo-app\node_modules\mongodb\lib\operations\connect.js:279:21
at QueryReqWrap.callback (C:\Users\Abhay\Desktop\todo-app\node_modules\mongodb\lib\core\uri_parser.js:56:21)
at QueryReqWrap.onresolve [as oncomplete] (dns.js:202:10)
[nodemon] app crashed - waiting for file changes before starting...
let express = require('express')
let mongodb = require('mongodb')
let app = express()
let db
let connectionString = 'mongodb+srv://todoAppUser:kTL7PYesKzfB6FMz#cluster0.fif5n.mongodb.net/TodoApp?retryWrites=true&w=majority'
mongodb.connect(connectionString, {useNewUrlParser: true, useUnifiedTopology: true}, function(err, client) {
db = client.db()
app.listen(3000)
})
It seems you're trying to use the static connect method of MongoClient to make a connection to your db, but you are not using the MongoClient class itself.
To connect to any db, you will need a connected instance of MongoClient. Using the static connect method, you can achieve it in the following way:
const mongodb = require("mongodb");
const connectionURL = "mongodb+srv://your-connection-srv-here"
const dbName = "your_db_name"
//get MongoClient
const MongoClient = mongodb.MongoClient;
let db = null;
MongoClient.connect(connectionURL,{
useNewUrlParser: true,
useUnifiedTopology: true
},(err,connectedClient) => {
if(err){
throw err;
}
//connectedClient will be the connected instance of MongoClient
db = connectedClient.db(dbName);
//now you can write queries
db.collection("your_collection").find({}).toArray()
.then(r => {
console.log(r);
}).catch(e => {
console.error(`ERROR:`,e);
})
})
However, using callbacks will be quite cumbersome. As per the docs linked above, most functions in the MongoDb driver for Node.js will return a promise if a callback function is not passed, which is very convenient. Using this, you can write a function which return a promise that resolves a connected instance to your db.
const MongoClient = require('mongodb').MongoClient;
/*
we draw the connection srv and the db name from the config to return just one instance of that db.
Now this function call be called wherever a connection is needed
*/
const getDbInstance = (config) => new Promise((resolve,reject) => {
const client = new MongoClient(config.dbUrl, {
useNewUrlParser: true,
useUnifiedTopology: true
});
client.connect((error) => {
if(error){
console.error(error);
reject(error);
}
let db = client.db(config.dbName);
resolve(db);
})
})
const doSomeDbOperations = async() => {
//hardcoding it here, but this config will probably come from environment variables in your project
const config = {
dbUrl: "mongodb+srv://your-connection-srv-here",
dbName: "your_db_name"
};
try{
const db = await getDbInstance(config);
//do whatever querying you wish here
}catch(e){
console.error(`ERROR: `,e);
}
}
doSomeDbOperations();

How to connect NodeJs to an Atlas mongodb Cluster [duplicate]

This question already has answers here:
db.collection is not a function when using MongoClient v3.0
(13 answers)
Closed 3 years ago.
I'm setting up a new app using an Atlas Database with node and all i get is an error saying " MongoError: MongoClient must be connected before calling MongoClient.prototype.db".
const uri = "mongodb+srv://alberto:pass#lel-kicis.mongodb.net/test";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("students")
.then(db => console.log('DB conectada'))
.catch(err => console.log(error));
});
If you look at the mongodb connector docs, the syntax for MongoClient is new MongoClient(url, options, callback). The signature for the callback is (err, client) => { //body }.
If you don't pass in the optional callback, you get and instance of MongoClient (which is the case here). The connect method also expects the same callback signature, so your connection should be like:
const instance = new MongoClient(uri, { useNewUrlParser: true });
// notice 'client' in the callback
instance.connect((err, client) => {
if (err) console.log('failed to connect')
else {
console.log('connected')
const collection = client.db("test").collection("students")
...
}
});
mongodb connector also support promise, so you can also do:
// connection is a promise
const connection = instance.connect()
connection.then((err, client) => { // etc })
Using mongoose and mongodb-uri :
Here is the way I initialise the connection :
const mongoose = require('mongoose')
const uriUtil = require('mongodb-uri')
// Create a new connection
mongoose.Promise = global.Promise
// mongoose.set('debug', DEBUG)
const dbURI = uriUtil.formatMongoose(process.env.MONGO_URI)
const options = {
autoIndex: DEBUG,
autoReconnect: true,
useNewUrlParser: true
}
const conn = mongoose.createConnection(dbURI, options)
conn.on('open', () => console.log('DB connection open'))
conn.on('error', err => console.log(`DB connection error : ${err.message}`, err))
conn.on('close', () => console.log('DB connection closed'))
module.exports = conn
Using the connection string provided by mongoDb for driver node.js version 3.0 or later.
You are missing to initiate the mongo client.
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://alberto:pass#lel-kicis.mongodb.net/test";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("students")
.then(db => console.log('DB conectada'))
.catch(err => console.log(error));
});
Also, Atlas generate the initial connection code block for you. Follow the below steps.
Clck on connect button
Select Connect Your Application from the next window
On the next window, Select NodeJs as driver and select the required version. Also, select Full driver example for full code block
Now copy the code and use it directly.

Categories

Resources