Cannot retrieve all entries from MongoDB collection - javascript

i'm trying to retrieve all entires from mongo yet I keep on getting an error that I couldn't find any while having there are some entries.
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const dbName = 'toy_db';
tryMongo();
function tryMongo() {
MongoClient.connect(url, (err, client) => {
if (err) return console.log('Cannot connect to DB');
console.log('Connected successfully to server');
const db = client.db(dbName);
const collection = db.collection('toy');
collection.find().toArray((err, docs) => {
if (err) return console.log('cannot find toys');
console.log('found these:');
console.log(docs);
});
client.close();
});
}
this is the error i'm getting :
Server listening on port 3030!
Connected successfully to server
cannot find toys
I have also added a picture of mongo
appreciating any kind of help!

You are closing mongo connection before you get response from server. Move client.close(); inside toArray callback.
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const dbName = 'toy_db';
tryMongo();
function tryMongo() {
MongoClient.connect(url, (err, client) => {
if (err) return console.log(err);
console.log('Connected successfully to server');
const db = client.db(dbName);
const collection = db.collection('toy');
collection.find().toArray((err, docs) => {
if (err) {
console.log(err);
} else {
console.log('found these:');
console.log(docs);
}
client.close();
});
});
}

Related

Executing mysql query suspends process and code after won't be executed

I'm asking help as I'm trying to get 2 different sets of data from a mysql db. I've written 2 separate functions using promisify. Unfortunately when I execute this node file only the first function gets executed and shown in the console. After some debugging I think it's await query(...) the cause of this problem. After this query (the one inside getSources() ) is executed, the process is suspended and I've got to terminate it. I also tried using mysql2 and moving con.end() to a different line unsuccesfully. Of course any help we'll be appreciated.
import mysql from 'mysql';
import { promisify } from 'util';
import express from 'express';
import path from 'path';
const sql = "SELECT `id` FROM `probes` WHERE `country`= ?";
const sql1 = "SELECT `fqdn` FROM `anchors` WHERE `country`= ?";
let sources = []; //sources
let targets = []; //targets
const app = express();
const __dirname = path.resolve();
app.use(express.urlencoded( {extended: true} ));
app.use(express.static(__dirname));
const con = mysql.createConnection({
host:'localhost',
user:'root',
password:'',
database:'probes&anchors'
});
const query = promisify(con.query).bind(con);
const getSources = async (from) => {
console.log("I'm in getSources");
con.connect((err)=>{
if(err){
console.log("Connection not proper");
}else{
console.log("connected");
}
});
await query(sql, [from], (err, rows) => {
console.log("error in query: "+ err);
if (err) throw err;
rows.forEach((v) => {
sources.push(v.id);
});
console.log(sources);
con.end(err => {
if(err) console.log(`${err.toString()}`)
console.log('con ended')
});
});
};
const getTargets = async (to) => {
console.log("I'm in getTargets");
con.connect((err)=>{
if(err){
console.log("Connection not proper");
}else{
console.log("connected");
}
});
await query(sql1, [to], (err, rows) => {
console.log(err);
if (err) throw err;
rows.forEach((v) => {
targets.push(v.fqdn);
});
con.end(err => {
if(err) console.log(`${err.toString()}`)
console.log('con ended')
});
});
};
app.post('/', async function(req,res){
await getSources(req.body.from_country);
await getTargets(req.body.to_country);
console.log(sources);
console.log(targets);
res.send("Loaded");
});
app.get('/', (req, res) => res.send('Hello World! From Node.js'));
app.listen(8000, () => console.log('Example app listening on port 8000!'));

Node JS MongoDB collection.find.toArray returns no value

I'm building a website that lets people write sticky notes and print it to them on the screen. I want to store the sticky notes inside a mongoDB with a db called stickyNotes and a collection called stickyNotes which currently has two documents.
I have a variable called stickyNotes which suppose to get the documents from the stickyNotes collection on the db but when I use the collection.find.toArray from the mongodb library to enter the documents to the stickyNotes variable in an asynchronous way, it shows an empty array value.
This is my server.js file:
const express = require("express");
const mongo = require("mongodb").MongoClient;
const app = express();
let stickyNotes = [];
//mongodb get all sticky notes
const mongoUrl = "mongodb://localhost:27017";
mongo.connect(mongoUrl, { useNewUrlParser: true }, async function(
err,
connection
) {
if (err) {
console.error(err);
} else {
console.log("Succesfully connected to the database");
const db = connection.db("stickyNotes");
const stickyNotesCollection = db.collection("stickyNotes");
stickyNotes = await stickyNotesCollection.find({}).toArray();
}
connection.close();
});
console.log(stickyNotes);
app.use(express.static("./src/public"));
app.get("/sticky-notes", (req, res) => {
console.log("Got a request for sticky notes");
res.json(stickyNotes);
});
const port = 3000;
app.listen(port, () => {
console.log(`App is running on port ${port}`);
});
Can try with:
stickyNotesCollection.find({}, (err, result) => {
if (err) throw err;
stickyNotes = result;
});
or find result in array:
collection.find().toArray(function(err, result) {
console.log(result);
});
or iterate:
collection.find().each(function(err, result) {
//once result
});

Every second run throws: MongoError: Topology was destroyed

I am building a REST API but every second time I load my site I get a MongoError: Topology was destroyed. Can someone help me fixing this? I have a feeling that there is something wrong with the asynchronous running.
const client = new MongoClient(apiconfig.mongoUrl, {
useNewUrlParser: true
});
app.get("/api/:object", (req, res) => {
mongodb(req.params["object"], async (collection: Collection) => {
if (collection !== undefined) {
let result = await collection.find().toArray();
res.send(result);
}
else {
res.sendStatus(404);
}
});
});
const mongodb = (coll: string, operation: (collection: Collection) => Promise<void>) => {
client.connect((err) => {
const db = client.db("VaorraJS");
db.collections().then((collections) => {
operation(collections.find((collection) => collection.collectionName === coll)).then(() => {
client.close();
});
}).catch((error) => {
console.log("ERROR: " + error);
});
});
}
app.listen(5000);
I would suggest the use Mongoose
you are creating DB connection for every request, which is not the correct way
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = '<some db>';
// Use connect method to connect to the server
let db;
MongoClient.connect(url, function (err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
db = client.db(dbName);
});
app.get("/api/:object", async(req, res) => {
const collection = db.collection(req.params["object"]);
let result = await collection.find().toArray();
res.send(result);
});

TypeError: db.collection is not a function with user password restriction

I have a university project where I can ssh to a server that has a mongodb with fixed database/username/password. I imported a collection and now want to read it out with nodejs for testing. After starting it with node server.js it returns "Connected correctly to server" into console but then I get a TypeError: db.collection is not a function
What is wrong? Thanks
var MongoClient = require('mongodb').MongoClient;
const user = encodeURIComponent('x');
const password = encodeURIComponent('y');
const authMechanism = 'DEFAULT';
// Connection URL
const url = `mongodb://${user}:${password}#localhost:27017/database?authMechanism=${authMechanism}`;
MongoClient.connect(url, function(err, db) {
console.log("Connected correctly to server");//works
var cursor = db.collection('locations').find();//throws error
cursor.each(function(err, doc) {
console.log(doc);
});
});
Try this way:
var MongoClient = require('mongodb').MongoClient;
const user = encodeURIComponent('x');
const password = encodeURIComponent('y');
const authMechanism = 'DEFAULT';
// Connection URL
const url = `mongodb://${user}:${password}#localhost:27017/database?authMechanism=${authMechanism}`;
MongoClient.connect(url, function(err, db) {
if(err){
console.log("Connection failed");
}
else{
console.log("Connected correctly to server");
var cursor = db.collection('locations');//same error
cursor.find({}).toArray(function(err,docs){
if(err){
console.log("did'nt find any!")
}
else{
console.log(docs)
}
});
}
});
Got it working after all:
var MongoClient = require('mongodb').MongoClient;
const user = encodeURIComponent('x');
const password = encodeURIComponent('y');
const authMechanism = 'DEFAULT';
// Connection URL with and without authentication
const url = `mongodb://${user}:${password}#localhost:27017/database?authMechanism=${authMechanism}`;
//const url = `mongodb://localhost:27017/`;
MongoClient.connect(url, (err, db) => {
if(err) throw err;
console.log("connect works");
let database = db.db('database');
database.collection('users').find().toArray((err, results) => {
if(err) throw err;
results.forEach((value)=>{
console.log(value);
});
})
});

What is wrong with my database file?

this is my database.js file:
const MongoClient = require('mongodb').MongoClient;
const db = function(){
return MongoClient.connect('mongodb://localhost:27017/users', (err, database) => {
if (err) return console.log(err);
return database;
});
}
module.exports = db;
I icluded it to my server.js like this:
var db = require('./database');
but when I want to use it like this
db().collection('orders')
I am getting a TypeError (Cannot read property 'collection' of undefined)
Edit: sorry, I made an issue during writing this question of course I used db().collection
The issue is with your export, and misunderstood behavior of node's callbacks.
const MongoClient = require('mongodb').MongoClient;
const db = function(){
return MongoClient.connect('mongodb://localhost:27017/users', (err, database) => {
// this is inside a callback, you cannot use the database object outside this scope
if (err) return console.log(err);
return database; // this database object is what you should be exporting
});
}
module.exports = db; // You are exporting the wrong variable
One way to fix this is (may not be the best) to export the database object that we receive in the callback. Example:
const MongoClient = require('mongodb').MongoClient;
let database = null;
MongoClient.connect('mongodb://localhost:27017/users', (err, db) => {
if (err) return console.log(err);
database = db;
});
module.exports = database;
And now you can use the db, but with a null check.
var db = require('./database');
if (db !== null) {
db.collection('orders').find({}, (err, docs) => {
if (err) return console.log(err);
console.log(docs);
});
}
But this may lead to connection being established again and again when you require the database.js file (I am not sure about this). A better approach would be:
const MongoClient = require('mongodb').MongoClient;
let database = null;
const connect = () => {
if (database !== null) return Promise.resolve(database);
return new Promise((resolve, reject) => {
MongoClient.connect('mongodb://localhost:27017/users', (err, db) => {
if (err) return reject(err);
database = db;
resolve(database);
});
});
};
module.exports = connect;
and then use it like:
var dbConnect = require('./database');
dbConnect().then((db) => {
db.collection('orders').find({}, (err, docs) => {
if (err) return console.log(err);
console.log(docs);
});
}).catch((err) => {
console.error(err);
});

Categories