Hello I'm learning mongoDB and trying to print out my database query results to the browser.
I have a program working where it writes the output JSON to the console using console.log()
Is there a way to use res.send() (using express) or response.write() and response.send() to simply output the raw JSON data that the database query gets?
In other words, How can I make my database invokatation return a string?
// Use connect method to connect to the server
var invokeDatabase = function() {
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to database server");
findDocuments(db, function() {
findDocumentsFiltered(db, function() {
db.close();
});
});
});
};
//routes
app.get('/', function(req, res) {
console.log("Someone connected.")
res.send("accessing database... " + invokeDatabase())
//res.send('Welcome G')
})
This example may help you to understand
// Use connect method to connect to the server
var invokeDatabase = function(callback) {
MongoClient.connect(url, function(err, db) {
//assert.equal(null, err);
if(err) {
console.log("Unable to connect database");
callback(err, null);
return;
}
console.log("Connected successfully to database server");
findDocuments(db, function() {
findDocumentsFiltered(db, function(err, data) {
callback(err, data);
db.close();
});
});
});
};
//Added for demo. Here users is collection
var findDocumentsFiltered = function(db, callback) {
db.collection('users').find({}).toArray(function(err, userList) {
callback(err, userList);
});
};
//routes
app.get('/', function(req, res) {
console.log("Someone connected.")
invokeDatabase(function(err, data) {
if(err)
res.status(500).json({error: err});
else
res.json(data);
}))
//res.send('Welcome G')
})
Related
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();
});
});
};
i'm trying to learn how to build an api with Node and Express.js. I've found the next step by step: click here
And created a very similar version but with my data:
var express = require("express");
var bodyParser = require("body-parser");
var sql = require("mssql");
var app = express();
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
next();
});
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
var dbConfig = {
user: "myUser",
password: "myPass",
server: "myServer",
database: "MyDB"
};
var executeQuery = function(res, query){
sql.connect(dbConfig, function (err) {
if (err) {
console.log("Error al conectarse a la base :- " + err);
res.send(err);
}
else {
// create Request object
var request = new sql.Request();
// query to the database
request.query(query, function (err, res) {
if (err) {
console.log("Error al correr query en la base :- " + err);
res.send(err);
}
else {
res.send(res);
}
});
}
});
}
//GET API
app.get("/api/ApiRequestData", function(req, res){
var query = "select * from [RequestData]";
executeQuery (res, query);
});
After create the server.js doc, executed with npm install and run with node server.js, i opened on postman using the next url: http://localhost:8080/api/ApiRequestData and get an error message: "Could not get any response". On the Node command prompt i get the message:
TypeError: res.send is not a function
at C:\Users\API\server.js:43:44
at C:\Users\API\node_modules\mssql\lib\main.js:1588:20
at Request.userCallback (C:\Users\API\node_modules\mssql\lib\tedious.js:853:61)
at Request.callback (C:\Users\API\node_modules\tedious\lib\request.js:33:27)
at Connection.message (C:\Users\API\node_modules\tedious\lib\connection.js:1179:27)
at Connection.dispatchEvent (C:\Users\API\node_modules\tedious\lib\connection.js:519:45)
at MessageIO. (C:\Users\API\node_modules\tedious\lib\connection.js:439:23)
at emitNone (events.js:106:13)
at MessageIO.emit (events.js:208:7)
at ReadablePacketStream. (C:\Users\API\node_modules\tedious\lib\message-io.js:92:15)
Someone knows why shows this message?
Hope you can help me.
You are shadowing res from line var executeQuery = function(res, query){... with res from line request.query(query, function (err, res) {.... Just rename the last res to something else and you won't get this error:
request.query(query, function (err, result) {
if (err) {
console.log("Error al correr query en la base :- " + err);
res.send(err);
}
else {
res.send(result);
}
});
You can use below query for fetching the records with hard coded query like.
I used same for my application.
sql.connect(config).then(() => {
return sql.query`select * from [dbo].[Customer]`
}).then(result => {
console.log(result)
}).catch(err => {
console.log(err);
})
Fetch the result using store procedure.
sql.connect(config).then(pool => {
return pool.request().input('Customerid', sql.Int, 2).execute("GetCustomerbyId")
}).then(result => {
console.log(result)
}).catch(err => {
console.log(err);
})
Trying to call database query from other javascript file in NodeJS.
Sample database file:
function addUser(user) {
connection.connect(function (err) {
if (err) {
console.error('Error connecting: ' + err);
}
console.log('Connected as id ' + connection.threadId);
});
var sql = "INSERT INTO `mobile`.`main` (`userId`) VALUES (?);"
var inserts = [user];
connection.query(sql, inserts, function (error, results) {
console.log('query');
if (error) {
return error;
} else {
console.log('Success Query');
return results;
}
});
connection.end(function (err) {
if (err) {
console.error('Error connecting: ' + err);
}
console.log('Connection closed!');
});
}
module.exports = addUser;
Sample main.js file:
app.get('/api/mysql/:user', function (req, res) {
var user = req.params.user;
addUsers(user)
res.json({
SQLResp: 'Query succes',
result: addUsers.result
});
});
How to get the result from the first file and use it as a response in the main js?
Welcome to stack overflow.
this is missing from your database file, How did you got connection there?
Here's a sample example over How to export database connection and use this to make calls from other files.
Also do read about callbacks-functions, Promises, async-await and Asynchronous functions of JavaScript. These are basics of JavaScript and also do go through NodeJS docs.
DB.js:
const MYSQL = require('mysql');
const connection = MYSQL.createConnection({
host: 'localhost', // url of db
user: 'root',
password: 'root',
database: 'dbName'
});
module.exports = connection;
Now will use this connection from other file to call database.
app.js:
const db = require('./DB'); // Path of your db connection file. I named it DB.js on same level as of app.js
function addUser(user) {
// Since database calls are async in NodeJS thus using promises.
return new Promise ( (resolve, reject) => {
db.connect(function (err) {
if (err) {
console.error('Error connecting: ' + err);
return reject(err);
}
console.log('Connected as id ' + connection.threadId);
});
var sql = "INSERT INTO `mobile`.`main` (`userId`) VALUES (?);"
var inserts = [user];
db.query(sql, inserts, function (error, results) {
console.log('query');
if (error) {
return reject(err);
} else {
console.log('Success Query');
return resolve(results);
}
});
db.end(function (err) {
if (err) {
console.error('Error connecting: ' + err);
}
console.log('Connection closed!');
});
});
}
app.get('/api/mysql/:user', function (req, res) {
var user = req.params.user;
addUsers(user)
.then (result => { // When addUsers will resolve promise will be in then.
res.json({
SQLResp: 'Query succes',
result: result
});
})
.catch(err => { // If promise is rejected then on catch
res.json({
SQLResp: 'Query err',
result: err
});
});
});
You need to pass a callback into your addUser function, and call it inside your connection.query with the results.
Currently, when you return, you're returning inside another callback, which means the data is more or less being thrown away.
That will also let you handle all of the error cases in a way you can tell the user about.
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...
});