Mongodb-mongoose collections undefined - node.js

I am new and practicing Mongoose and Mongodb connection. I successfully connect my database to my local machine and get log confirmations. I am trying get my collection without passing mongoose Schema but I am getting error: TypeError: Cannot read property 'collection' of undefined. I don't know what I am doing wrong.
Here is code:
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const { Schema } = mongoose;
const app = express();
mongoose
.connect(process.env.MONGODB_URI, {
useUnifiedTopology: true,
useNewUrlParser: true
})
.then(() => console.log("DB Connected!"))
.catch(err => {
console.log(err);
});
var connection = mongoose.connection;
connection.db.collection("bags", function (err, collection) {
collection.find({}).toArray(function (err, data) {
console.log(data); // it will print your collection data
})
});

You're trying to access the DB before the connection is established to MongoDB. ( Connecting to DB is asynchronous code )
Also, Read - https://javascript.info/async-await
mongoose
.connect(process.env.MONGODB_URI, {
useUnifiedTopology: true,
useNewUrlParser: true
})
.then(() => {
console.log("DB Connected!");
var connection = mongoose.connection;
// run logic once db is connected.
connection.db.collection("bags", function (err, collection) {
collection.find({}).toArray(function (err, data) {
console.log(data); // it will print your collection data
})
});
})
.catch(err => {
console.log(err);
});
Or
mongoose.connection.on('connected', err => { // will trigger connected event when connected to MongoDB
var connection = mongoose.connection;
connection.db.collection("bags", function (err, collection) {
collection.find({}).toArray(function (err, data) {
console.log(data); // it will print your collection data
})
});
});

Related

Connecting MongoDB with NodeJS results in "collection is not a function" TypeError

I am new to MongoDB. I am trying to establish a connection between my ExpressJS server and a MongoDB database. This is my code:
const PRIMARY_SERVER = "mongodb://localhost:27017/";
const { MongoClient } = require("mongodb");
const client = new MongoClient(PRIMARY_SERVER, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function connect() {
try {
await client.connect();
console.log("Connected");
module.exports = client.db("mydb");
const app = require("../app.js");
app.listen(5050);
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
connect();
The connection gets established fine but as soon as this function gets called:
router.post("/pushbill", async function (req, res) {
databaseConnection.collection("bills").insertOne(object, function(err, result){
if(err) {
result.status(400).send("Error inserting matches.");
} else {
console.log(`Added a new match with id 0`);
result.status(204).send();
}
});
});
I get this error: "(node:17984) UnhandledPromiseRejectionWarning: TypeError: databaseConnection.collection is not a function".
I've searched for this issue for a few hours now but can't seem to find a solution. I tried several different methods of connecting my database, one of them included this ticket: db.collection is not a function when using MongoClient v3.0 . I am using MongoDB 4.13.0.
It was an issue where the connection to MongoDB kept closing before any operation was made.
I solved this issue by opening connection each time a request gets made, and waiting till the operations are done with a callback or in this case a timeout.
const connDB = async(operations, response) => {
try{
const client = new MongoClient('mongodb ip here', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
await client.connect().then(function(result){
}).catch(function(err){
throw err;
});
const db = await client.db('my_db');
operations(db);
setTimeout(() => {client.close()}, 1500)
} catch(err){
console.error(err);
response.status(500).send({ message: 'Error connecting or operating the database', error: err});
await client.close();
}
}

MongoDB won't insert document

I don't know what I am doing wrong, however the following is not working.
I have the database connecting however when I try and use the connection it won't let me.
index.js
config = require('./config');
const express = require('express');
const app = express();
const mDB = require('./components/mongodb').connect(config.dbUri);
app.get('/testDB', (req,res) =>{
const user = { name: 'John', age: 30 };
mDB.insertOne(user, (error, result) => {
if (error) {
console.log(error);
return;
}
console.log(`Successfully inserted user: ${result.insertedId}`);
res.send("inserted");
});
});
app.listen(3000, () => {
console.log('App listening on port 3000');
});
./components/mongodb.js
const MongoClient = require('mongodb').MongoClient;
module.exports.connect = (uri) => {
MongoClient.connect(uri, { useNewUrlParser: true }, (err, client) => {
if (err) {
console.error(`MongoDB connection error: ${err}`);
process.exit(1);
}
console.log('Successfully connected to MongoDB');
module.exports.client = client;
});
};
module.exports.insertOne = (collection, document) => {
module.exports.client.db('PodToo').collection(collection).insertOne(document, (error, result) => {
if (error) {
console.log(error);
return;
}
console.log(`Successfully inserted document: ${result.insertedId}`);
});
};
I get the Successfully connected to MongoDB
But then when I try and use it I am receiving
TypeError: Cannot read properties of undefined (reading 'insertOne')
In the danger of stating the obvious: You are missing the first argument to your insertOne function, the collection name.
Did you mean
mDB.insertOne('users', user, ...
?

Mongoose is returning empty array while fetching data from MongoDb Atlas. Why?

App.js file->Connection is established successfully, but in find() callback,data is empty[]
const express = require('express');
const mongoose = require('mongoose');
const Users = require('./users');
const app = express();
mongoose.connect("mongodb+srv://sanjeev:**pass**#cluster0.ckywrym.mongodb.net?retryWrites=true&w=majority/sanjeevDb",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => console.log("connection established successfully"));
Within find callback I am getting empty array in data
Users.find({}, (error, data) => {
if (error)
console.log("Error: ", error);
console.log(data)
});
users.js - defining the schema as same on mongoDb Atlas
const mongoose = require('mongoose');
let userSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: String,
email: String,
country: String
});
module.exports= mongoose.model('userCollect', userSchema);
enter image description here
you are logging data even when there is error. do this
Users.find({}, (err, data) => {
if (err){
console.log(err);
} else {
console.log(data);
})
or
//with async (recommended)
try {
const users = await Users.find({});
console.log(users);
} catch (err) {
console.log(err);
}

Can .save() Mongoose Schema only one time, next i need to restart node.js server to save it again

i have this problem. First i start Mongoose connecting it to Atlas Db.
dbConfig.js
const mongoose = require('mongoose');
mongoose.connect('mongodb://myMongoDbOnAtlas?retryWrites=true&w=majority";',
{
useUnifiedTopology: true,
useNewUrlParser: true,
}
);
mongoose.connection.once('open', () => {
console.log('Connected to db');
}).on('error', (error) => {
console.warn('Error: ' + error);
})
module.exports = mongoose;
/db/schema/offices.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const OfficeSchema = new Schema({
// Some Properties
});
const Office = mongoose.model('offices', OfficeSchema);
module.exports = Office;
addOfficeController.js
const Office = require('../db/schema/office');
const mongoose = require('../db/dbConfig');
const addOffice = async (req, res, next) => {
const office = req.body;
let newOffice = new Office(office);
newOffice.save((err, data) => {
if(err){
res.status(400).send(err);
console.log(err);
}else{
res.status(201).send(data);
}
mongoose.connection.close();
})
};
module.exports = {
addOffice
};
Now when i start the Node server my controller works fine and save on db the data, but if i try to do it again the callback is an error: MongooseError: Operation offices.insertOne() buffering timed out after 10000ms
Ok i solved in this way
const mongoose = require('mongoose');
const startDb = () => {
mongoose.connect('myMongoDb#cluster0.hazp8.mongodb.net/db?retryWrites=true";',
{
useUnifiedTopology: true,
useNewUrlParser: true,
}
);
mongoose.connection.once('open', () => {
console.log('Connected to db');
}).on('error', (error) => {
console.warn('Error: ' + error);
})
}
module.exports = startDb;
I exported a function that starts the connection to db, and after save() I close the connection.

Problems returning database information with MongoDB, Node.js and Express

const express = require('express');
const second = express();
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myorders';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("The connection was awesome");
const db = client.db(dbName);
second.get('/Students',(req,res) => {
const findStudents = function(db,call) {
const collection = db.collection('Students');
collection.find({}).toArray(function (err,result) {
if (!err) {
res.send(result)
} else {
console.log(err);
call(err);
}
});
}
});
client.close();
});
second.listen(3200,() => {
console.log('We got it running');
});
module.exports = second;
So I am trying to display my mongodb information on a web server and here are the screen shots
Above is the MongoDB collection of students and marks
And above is my Node.js command prompt and I get a deprecation warning. Is there any way I can fix it? Is there any other error why I can't display the information in the web server?
To fix this deprecation warning, all you need is to do what it says: pass the option useNewUrlParse: true in the options object to MongoClient.connect. So the line where you connect to MongoDB database would be:
MongoClient.connect(url, { useNewUrlParse: true }, function(err, client)
Anyway, this is just a warning, not a blocking error, and your app is running. I think there are several things wrong in your code, mainly that you declare the function to be executed on the GET to /Students within the callback of the connection to the database and that you close that connection so that the data could no longer be accessed. I think this code should work for you:
const express = require('express');
const second = express();
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const dbUrl = 'mongodb://localhost:27017';
const dbName = 'myorders';
let db;
MongoClient.connect(url, (err, client) => {
assert.equal(null, err);
db = client.db(dbName);
console.log("The connection was awesome");
});
second.listen(3200, () => {
console.log('We got it running');
});
second.get('/Students', (req, res) => {
db.collection('Students').find().toArray((err,result) => {
if (!err) {
res.send(result)
} else {
console.log(err);
res.status(500).send(err);
}
});
});
EDIT:
If you think that the database should not be always opened, yous should connect and close at each request to /Students, this way:
second.listen(3200, () => {
console.log('We got it running');
});
second.get('/Students', (req, res) => {
MongoClient.connect(url, (err, client) => {
if (err) return res.status(500).send(err);
db = client.db(dbName);
db.collection('Students').find().toArray((err,result) => {
if (!err) {
res.send(result)
} else {
console.log(err);
res.status(500).send(err);
}
client.close();
});
});
});

Resources