MongoDB client in Node - javascript

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})

Related

Unable to connect to my database from my node js application

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))

How do i define 'db' i trying connecting to connect mongodb to nodejs

I'm trying to connect nodejs to mongodb database but lots of errors occur
let express = require ('express')
let mongodb = require ('mongodb')
let app = express()
let db
let connectionString =
'mongodb+srv://Akshay:UYLu5kVEYCXtUdpY#cluster0.qxvlm.mongodb.net/TodoApp?
retryWrites=true&w=majority'
mongodb.connect(connectionString, {useNewUrlParser: true, useUnifiedTopology: true}, function
(err, client){
db = client.db()
app.listen(3000)
})
My controller:
app.post('/create-item', function(req, res) {
db.collection('item').insertOne({text: req.body.item}, function() {
res.send("Thanks for submitting the form.")
})
Error message:
'db' is not defined
I think you have problem with your file structure. Probably you are not including db to your controller.
Please, create a new file and call it database.js
Design your database.js like this:
const { MongoClient } = require('mongodb');
class DatabaseSingleton {
constructor() {
const uri = process.env.DB_CONNECTION_STRING;
this.client = new MongoClient(uri, { useUnifiedTopology: true });
this.isConnected = false;
this.client.connect().then(()=>{this.isConnected = true});
}
/**
* Returns the promise of a Database object. (It waits until a connection to the database was successful)
* #returns {Promise<any>}
*/
get mongo() {
return new Promise(async (resolve, reject) => {
let timedOut = false;
setTimeout(()=>{timedOut = true}, 300 * 1000);
while (!this.isConnected && !timedOut){
await new Promise((resolve)=>{setTimeout(()=>{resolve()}, 100)}) // Snooze the thread for 100ms without blocking the event loop
}
if (timedOut){
reject(false);
} else {
resolve(this.client.db());
}
})
}
}
// *** Setting up singleton ***
const SINGLETON_KEY = Symbol.for("MyProject.MyDB");
var globalSymbols = Object.getOwnPropertySymbols(global);
var hasInstance = (globalSymbols.indexOf(SINGLETON_KEY) > -1);
// If there is no instance of the singleton, make the instance.
if (!hasInstance){
global[SINGLETON_KEY] = new DatabaseSingleton()
}
// Define a function to retrieve a singleton instance
var singleton = {};
Object.defineProperty(singleton, "instance", {
get: function(){
return global[SINGLETON_KEY];
}
});
// The singleton should never be changed
Object.freeze(singleton);
module.exports = singleton;
// *** End of singleton setup ***
And convert your controller something like:
const database = require('./database').instance;
app.post('/create-item', function(req, res) {
const db = await database.mongo; // Call db here
db.collection('item').insertOne({text: req.body.item}, function() {
res.send("Thanks for submitting the form.")
})
Try this.
Create a db.js file to setup database connection only and export the client.
const dotenv = require("dotenv");
dotenv.config();
const mongodb = require("mongodb");
mongodb.connect(
process.env.CONNECTIONSTRING,
{
useNewUrlParser: true,
useUnifiedTopology: true,
},
(err, client) => {
module.exports = client
const server = require("./server");
server.listen(process.env.PORT);
}
);
Now in your server.js file require the client that are exported.
const client = require('./client')
now use this as
client.db('databaseName').collection('item').insertOne({})

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();

Create in-memory database using mongodb-memory-server and mongoose

I want to create a in-memory database using mongodb-memory-server and mongoose. Can anyone show the correct way to do it.
From version 7 and above
// this no longer works
const mongo = new MongoMemoryServer();
const uri = await mongo.getUri(); // ERROR: instance not started
// it is now
const mongo = await MongoMemoryServer.create();
const uri = mongo.getUri();
https://nodkz.github.io/mongodb-memory-server/docs/guides/migration/migrate7/#no-function-other-than-start-create-ensureinstance-will-be-starting-anything
Read the documentation here and came accross the following solution.
const mongoose = require('mongoose');
const { MongoMemoryServer } = require('mongodb-memory-server');
const mongoServer = new MongoMemoryServer();
mongoose.Promise = Promise;
mongoServer.getUri().then((mongoUri) => {
const mongooseOpts = {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true,
};
mongoose.connect(mongoUri, mongooseOpts);
mongoose.connection.on('error', (e) => {
console.log(e);
});
mongoose.connection.once('open', () => {
console.log(`MongoDB successfully connected to ${mongoUri}`);
});
});
module.exports=mongoose.connection;

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