I want to read out a mongodb database with API URLS. When I access /showdb in my browser the json is only display after the second refresh. How can I get it the first time? Thanks!
const express = require("express");
const app = express();
var mongo = require('mongodb');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
var resultdb;
function readDB() {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("simpledb");
dbo.collection("simplecollection").find().toArray(function(err, result) {
if (err) throw err;
resultdb = result;
db.close();
});
});
return resultdb;
};
//handle normal file requests etc.
app.use(express.static(__dirname + "/"));
app.get('/showdb', function(req, res) {
res.send(readDB());
});
app.listen(10008);
console.log("Server running on port: " + 10008);
What happens here is that you return resultdb without awaiting the db response. Hence why second call works due to your variable is getting updated after res has been sent. Try below
const url = "mongodb://localhost:27017/";
const mongoClient = new MongoClient(new Server(url, 27017));
async function readDB() {
mongoClient.open(function(err, db) {
if (err) throw err;
var dbo = db.db("simpledb");
const res = dbo.collection("simplecollection").find().toArray(function(err, result) {
if (err) throw err;
return result
});
mongoClient.close();
return await res
});
};
mongoClient.open(function(err, mongoClient) {
var db1 = mongoClient.db("mydb");
mongoClient.close();
});
Also, it's not a good practice to create connection for every task.
I would suggest to create a separate function to connect upon server start & then just use client.open() when you want to do db tasks
You need to use the callbacks you can't use synchronous code. Like this:
app.get('/', (req, res) => {
MongoClient.connect(url, (conn, err) => {
conn.db('foo').collection('bar').find().toArray((err, result) => {
res.send(result)
})
})
})
The callback functions are executed later that's why they are callbacks. If you want to write code that looks more like synchronous code look at promise API and await
The problem with code is that it does not wait for readDB() to finish it tasks with Mongodb and returns with empty as resultdb is just defined.
But when you call it once, after the request is served, readDB() would have received data from Mongodb and it will be set to resultdb. Next time when you call the api, you get the result processed in the previous api call and not the new one.
Try this -
app.get('/showdb', async function(req, res) {
const result = await readDB(); // here we are waiting for the results to finish using await.
res.send(result);
});
and your readDB function as -
async function readDB() { // making using of async here.
MongoClient.connect(url, function(err, db) {
if (err) throw err;
const dbo = db.db('simpledb');
dbo.collection('simplecollection').find().toArray(function(err, result) {
if (err) throw err;
resultdb = result;
return resultdb; // we can now return it.
db.close();
});
});
};
Note: Considering you're using an updated Node version with support for async - await
--- UPDATE ----
You can try this -
app.get('/showdb', async function(req, res) {
readDB(req, res);
});
function readDB(req, res) { // making using of async here.
MongoClient.connect(url, function(err, db) {
if (err){
res.send(err);
throw err;
}
const dbo = db.db('simpledb');
dbo.collection('simplecollection').find().toArray(function(err, result) {
if (err){
res.send(err);
throw err;
}
res.send(result)
db.close();
});
});
};
Related
This is in a mysql.js file:
const mysql = require('mysql');
const config = require('./config.json');
const con = mysql.createConnection({
host: config.dbhost,
user: config.dbuser,
password: config.dbpass,
database: config.dbname,
});
module.exports = {
findUser: function(email) {
const sql = 'SELECT * FROM users WHERE email = ' + mysql.escape(email);
con.connect(function(err) {
if (err) throw err;
console.log('Connected!');
con.query(sql, function(err, result) {
if (err) throw err;
return result[0].id;
});
});
},
};
then within my index.js file there is this:
const mysql = require('./mysql.js');
console.log(mysql.findUser('example#example.test'));
When the code is running, it outputs "undefined" and then "Connected!" after the db connection is made. Even though if I do a console.log on result[0].id it outputs 1, which is the correct id...
Question: How can I load the mysql.js file first before the function is called?
You need to wait for response cause its an asynchronous function.
Try using callback or promises.
Callback example:
mysql.findUser('example#example.test', function(res)){ console.log(res)});
module.exports = {
findUser: function(email, callback) {
const sql = 'SELECT * FROM users WHERE email = ' + mysql.escape(email);
con.connect(function(err) {
if (err) throw err;
console.log('Connected!');
con.query(sql, function(err, result) {
if (err) throw err;
callback(result[0].id);
});
});
},
i have a problem or not even a problem just not enough knowledge about this, so the question is simple, i have code
router.get('/', (ctx, next) => {
MongoClient.connect(url, {useNewUrlParser: true}, function (err, db) {
if (err) throw err;
let dbo = db.db("mydb");
let query = {address: "Highway 37"};
dbo.collection("customers").find(query).toArray((err, result) => {
if (err) throw err;
console.log(result)
db.close();
});
});
ctx.body = 'END OF FILE!!!';
});
console.log(result) i have my data and i need to response this data in ctx.body and i don;t know how to get result to my ctx.body, i am trying some options like create a variable and like let a, and a = result etc, however all hope is gone :)
Please any help will be appreciated, tnx a lot ^)
Your handler could look like this:
router.get('/', ctx => {
MongoClient.connect(..., (err, db) => {
...
dbo.collection("customers").find(query).toArray((err, result) => {
ctx.body = JSON.stringify(result)
})
})
})
I am working on a task with different modules.
I require a common mongodb connection for each and every module..
How can I write in some module and use in this because the db connection is also required in some other modules...
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
var dbo;
MongoClient.connect(url, function(err, db) {
if (err) throw err;
dbo = db.db("mydb");
});
router.post('/', function(req, res) {
dbo.collection("customers").find({"userid":req.body.userid}).toArray(function(err, result) {
if (err) throw err;
if(result.length>0){
res.send("username already taken please enter differnt username ")
}
else if(req.body.fname==undefined||(!validator.isAlpha(req.body.fname))){
res.send("please enter only alphabets as fname ")
}
else if(req.body.lname==undefined||(!validator.isAlpha(req.body.lname))){
res.send("please enter only alphabets as lname ")
}
else if(req.body.userid==undefined||(!validator.isAlphanumeric(req.body.userid))){
res.send("please enter only alphanemric as user name ")
}
else if(req.body.pwd==undefined||req.body.pwd.length<6){
res.send("please enter atleast 6 charcaters as password ")
}
else{
var bcrypt = require('bcryptjs');
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync(req.body.pwd, salt);
req.body.pwd=hash;
dbo.collection("customers").insertOne(req.body, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
});
res.send(req.body);
}
});
});
module.exports = router;
use can use node export and import, to that you can use mongodb connection instance in other modules also,
assuming dbo is variable where you want to store mongodb connection
export let dbo;
MongoClient.connect(url, function(err, db) {
if (err) throw err;
dbo = db.db("mydb");
});
you can assign db connection to dbo variable and and use it in whichever module you want
you have to create a middleware for your connection and then assign the db object to your request object ( req in your case )
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
router.use( (req,res,next) => {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
req.dbo = db.db("mydb");
next();
});
})
in your router.post("/", ...) you will do req.dbo.collection(...)
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
var dbo=null;
exports.conection=function(){
if(dbo!=null) return
MongoClient.connect(url, function(err, db) {
if (err) throw err;
dbo = db.db("mydb");
});
}
exports.get = function (){
return dbo;
}
i tried this and i use get method when ever i require this is working for me
I have successfully set up a database using mongodb, and I have managed to add new entries to my collection. However, when I use a similar method to delete, nothing happens.
Express.js code
router.post('/deleteproject', function(req, res) {
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/plugd';
MongoClient.connect(url, function(err, db) {
if (err) {
console.log("Unable to connect to server", err);
} else {
console.log('Connected to server');
var collection = db.collection('projects');
collection.remove(
{_id: new mongodb.ObjectID(req.body)}, function(err, result) {
if (err) {
console.log(err);
} else {
res.redirect("thelist");
}
db.close();
});
}
});
});
Jade code
h2.
ul
Projects
each project, i in projectlist
#project_list_item
a(href='#') #{project.owner} - #{project.project}
p #{project.ref1}
p #{project.ref2}
p #{project.ref3}
form#form_delete_project(name="deleteproject", method="post", action="/deleteproject")
input#input_name(type="hidden", placeholder="", name="_id", value="#{project._id}")
button#submit_project(type="submit") delete
I figured it out. Here is my fix for deleting data from a mongodb collection using a router in express.js.
Express.js
router.post('/deleteproject', function(req, res) {
var MongoClient = mongodb.MongoClient;
var ObjectId = require('mongodb').ObjectId;
var url = 'mongodb://localhost:27017/app';
MongoClient.connect(url, function(err, db) {
if (err){
console.log('Unable to connect to server', err);
} else {
console.log("Connection Established");
var collection = db.collection('projects');
collection.remove({_id: new ObjectId(req.body._id)}, function(err, result) {
if (err) {
console.log(err);
} else {
res.redirect("thelist");
}
db.close();
});
}
});
});
Jade code
extends layout
block content
h2.
Projects
ul
each project, i in projectlist
#project_list_item
a(href='#') #{project.owner} - #{project.project}
p #{project.ref1}
p #{project.ref2}
p #{project.ref3}
form#form_delete_project(name="deleteproject", method="post", action="/deleteproject")
input#input_name(type="hidden", placeholder="", name="_id", value="#{project._id}")
button#submit_project(type="submit") delete
The jade file is rendering to a page called 'thelist' that lists each item in the collection.
The form section handles the delete function for each item in the list.
This works for me as long as I keep Jade's indents happy :)
Try this and see if it works :
router.post('/deleteproject', function(req, res) {
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/plugd';
MongoClient.connect(url, function(err, db) {
if (err) {
console.log("Unable to connect to server", err);
} else {
console.log('Connected to server');
var collection = db.collection('projects');
collection.remove(
{_id: req.body}, function(err, result) {
if (err) {
console.log(err);
} else {
res.redirect("thelist");
}
db.close();
});
}
});
});
Since you're on MongoDB's Node.js Native Driver, you don't need to marshall _id inside ObjectId. You can directly specify the _id as string
I have a MongoDB connect call that crashes a heroku app..
I have been editing what was originally localHost code (was working perfectly) to work with Heroku MongoDb addons (like MongoLab), but how do I get someDBcollectionVariable to work with someDBcollectionVariable.find()
//MongoDB
var mongodb = require('mongodb');
var db;
var MONGODB_URI = process.env.MONGOLAB_URI;
var PORT = process.env.PORT;
var testColl;
function dbConnect() {
return mongodb.MongoClient.connect(MONGODB_URI, function(err, database) {
if(err) throw err;
db = database;
var testColl = db.collection('test');
app.listen(PORT);
console.log('Listening on port ' + PORT);
return testColl;
});
}
//calls then look like
app.post('/add', function (req, res) {
testColl.insert(
{
"title" : req.body.title,
"quantity" : parseInt(req.body.quantity)
},
function (err, doc) {
getAll(res);
});
});
//and getAll looks like this
function getAll(res) {
testColl.find().sort( { value: 1 } ).toArray(function (err, docs) {
res.json({docs: docs});
});
}
Before moving that code inside dbConnect(), testColl.find.. was generating a ResponseError because the connect code was completing before the variable could be set?
Returning a value from an asynchronous function makes no sense. To use the a value, you need to pass it to a callback function. The same goes for errors (you can't throw asynchronously). A fixed version of your code could look like:
//MongoDB
var mongodb = require('mongodb');
var db;
var MONGODB_URI = process.env.MONGOLAB_URI;
var PORT = process.env.PORT;
var testColl;
function dbConnect(callback) {
mongodb.MongoClient.connect(MONGODB_URI, function (err, database) {
if (err) {
return callback(err);
}
db = database;
database.collection('test', function (err, testColl) {
if (err) {
return callback(err);
}
app.listen(PORT);
console.log('Listening on port ' + PORT);
callback(null, testColl);
});
});
}
//calls then look like
dbConnect(function (err, testColl) {
if (err) {
return console.error(err.stack || err.message);
}
testColl.find...
});