I am building a VERY basic application to book a vehicle for hire, I am trying to access data from mongodb, which seems to work, as it outputs to the console on load. I cannot get this information to render in my view. If I set the render method outside of the function scope I cannot access the db data, if I set the render inside then the booking page never loads. I have been messing around with the same block of code for days now and I have had no joy.
//------------------------------------------------THE SET UP--------------------------------------------
// set node dependencies
let express = require("express");
let bodyParser = require("body-parser");
let mongoose = require("mongoose");
let connection = mongoose.connection;
let data = mongoose.connect("mongodb://localhost:27017/test")
// allow for encoding od POST DATA
let urlencodedParser = bodyParser.urlencoded({extended: false});
// set up app to extend express
let app = express();
// view engine
app.set("view engine", "ejs");
// static files
app.use("/assets", express.static("assets"));
//------------------------------------------------------------------------------------------------------
// REDIRECTS BASED ON URL
app.get('/', function(req,res){
res.render("index")
});
app.get('/booking', function(req,res){
res.render("booking",{qs:req.query})
});
app.post('/booking',urlencodedParser, function(req,res){
// Surround this with if !blacklisted to run inner code
if (req.body.blacklist !== "on"){
console.log(req.body);
// booking page takes age details and redirects/queries database accordingly
if (req.body.age >= 25){
connection.once('open', function () {
connection.db.collection("vehicles", function(err, collection){
collection.find({}).toArray(function(err, data){
console.log(data[0]._id); // it will print collection data
})
});
res.render("contact-success",{data:req.body})
connection.close();
});
}
else if (req.body.age < 25 && req.body.age > 17){
connection.once('open', function () {
connection.db.collection("vehicles", function(err, collection){
collection.find({}).toArray(function(err, data){
console.log(data[0]._id + "<25 message"); // it will print collection data
})
})
})
// THIS IS WHERE I WANT TO PASS THE DB DATA INTO.. so that it redirects to this page and filters the vehicles collection appropriately.
res.render("contact-failed",{data:req.body});
}
}
else{
console.log(req.body.firstName , req.body.lastName , "Has been blacklisted!")
res.render("blacklisted",{data:req.body});
}
// else if blacklisted redirect to a sorry, contact company page.
});
let port = 3000;
app.listen(port);
console.log("listening on port " + port);
Few things about your code -
You should call res.render() inside the call to mongodb.
connection.once('open', function () {
connection.db.collection("vehicles", function(err, collection){
collection.find({}).toArray(function(err, data){
console.log(data[0]._id); // it will print collection data
res.render("contact-success",{data:req.body})
});
});
connection.close();
});
You are not checking for errors. If you get any error with query, you will end up with no response to the request. So it's always better to check for errors.
if(err) {
// process the error and send appropriate message.
} else {
// send what you want to send to view.
res.render("contact-success",{data:req.body})
}
You have conditions for
if (req.body.age >= 25){
...
} else if (req.body.age < 25 && req.body.age > 17){
...
}
But there is nothing for plain else. So if any of the two conditions is not met, you will end up with no response to the request.
There should be one res.render() or res.send() in each of the conditions.
Related
I am trying to store data that has been filled out on form onto a database but it isn't storing.
The goal of this code is use a html file to make a from and once the data has been input, then it will be saved on the server and be displayed on table from the server side. I am currently only able to get the column names to be displayed but no input data-
var express = require('express');
var bodyParser = require("body-parser");
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database('myDBnaima3');
var app = express();
app.use(express.static('public_html'));
app.use(bodyParser.urlencoded({
extended: false
}));
app.post('/Contact', function (request, response, next) {
let name = request.body.Name;
let email = request.body.Email;
let message = request.body.Message;
//db.run('CREATE TABLE IF NOT EXISTS users (id INTEGER UNIQUE PRIMARY KEY NOT NULL, name TEXT NOT NULL, email TEXT NOT NULL);');
var statement = db.run(`INSERT INTO users (name, email, message) VALUES ("${name}","${email}","${message}");`);
console.log("Information Entered Succesfully!");
res.status(200).redirect('/');
});
app.get('/Contact', function (request, response, next) {
db.all('SELECT * FROM users;', function (error, rows) {
response.write(`<table border=1>`);
response.write("<tr>");
response.write(`<th>ID</th>`);
response.write(`<th>Name</th>`);
response.write(`<th>Message</th>`);
response.write("</tr>");
rows.forEach(row => {
response.write("<tr>");
response.write(`<td>${row["name"]}</td>`);
response.write(`<td>${row["email"]}</td>`);
response.write(`<td>${row["message"]}</td>`);
response.write("</tr>");
});
response.write("</table>");
});
});
app.listen(3000, function () {
console.log("Web server running at: http://localhost:3000 NAIMA IS HERE AGAIN!!!!");
console.log("Type Ctrl+C to shut down the web server");
});
You're not checking if your insert succeeds. It could be failing. You can check with a callback that will be run when the statement finishes, or when there's an error.
Pasting values straight into your SQL makes your code vulnerable to SQL Injection, a very common security problem, and other syntax errors. If, for example, your message contains a " it will break the insert.
You should instead use placeholders.
Putting them together, it's something like this.
db.run(
"INSERT INTO users (name, email, message) VALUES ($name,$email,$message)",
{ $name: name, $email: email, $message: message },
(error) => {
if( error ) {
console.log(`Insert failed: ${error}`);
res.status(500);
}
else {
console.log("Information Entered Succesfully!");
res.status(200).redirect('/');
}
}
);
Your code can be simplified by using db.each.
app.get('/Contact', function(request, response, next) {
response.write(`<table border=1>`);
response.write("<tr>");
response.write(`<th>ID</th>`);
response.write(`<th>Name</th>`);
response.write(`<th>Message</th>`);
response.write("</tr>");
db.each('SELECT * FROM users;', function(error, row) {
response.write("<tr>");
response.write(`<td>${row["name"]}</td>`);
response.write(`<td>${row["email"]}</td>`);
response.write(`<td>${row["message"]}</td>`);
response.write("</tr>");
});
response.write("</table>");
});
This is simpler and more efficient. db.all will slurp all the rows into memory at once potentially using a lot of memory if you have a lot of users. db.each will fetch them one at a time.
I'm using node and postgres, I'm new to writing async function, what I'm trying to do is a very simple query that will do a total count of records in the database, add one to it and return the result. The result will be visible before the DOM is generated. I don't know how to do this, since async function doesn't return value to callers (also probably I still have the synchronous mindset). Here's the function:
function generateRTA(callback){
var current_year = new Date().getFullYear();
const qry = `SELECT COUNT(date_part('year', updated_on))
FROM recruitment_process
WHERE date_part('year', updated_on) = $1;`
const value = [current_year]
pool.query(qry, value, (err, res) => {
if (err) {
console.log(err.stack)
} else {
var count = parseInt(res.rows[0].count) + 1
var rta_no = String(current_year) + '-' + count
callback(null, rta_no)
}
})
}
For the front-end I'm using pug with simple HTML form.
const rta_no = generateRTA(function (err, res){
if(err){
console.log(err)
}
else{
console.log(res)
}
})
app.get('/new_application', function(req, res){
res.render('new_application', {rta_number: rta_no})
});
I can see the rta_no in console.log but how do I pass it back to the DOM when the value is ready?
Based on the ajax call async response, it will update the div id "div1" when it gets the response from the Node js .
app.js
app.get("/webform", (req, res) => {
res.render("webform", {
title: "Render Web Form"
});
});
app.get("/new_application", (req, res) => {
// Connect to database.
var connection = getMySQLConnection();
connection.connect();
// Do the query to get data.
connection.query('SELECT count(1) as cnt FROM test ', function(err, rows, fields) {
var person;
if (err) {
res.status(500).json({"status_code": 500,"status_message": "internal server error"});
} else {
// Check if the result is found or not
if(rows.length==1) {
res.status(200).json({"count": rows[0].cnt});
} else {
// render not found page
res.status(404).json({"status_code":404, "status_message": "Not found"});
}
}
});
// Close connection
connection.end();
});
webform.pug - Via asynchronous call
html
head
script(src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js')
script.
$(document).ready(function(){
$.ajax({url: "/new_application", success: function(result){
$("#div1").html(result.count);
}});
});
body
div
Total count goes here :
#div1
value loading ...
That seems okay, I'm just not sure of this:
The result will be visible before the DOM is generated
This constraint defeats the purpose of async, as your DOM should wait for the returned value to be there. Instead of waiting for it you could just render the page and once the function returns and runs your callback update the value.
Also, perhaps it's worth having a look into promises
I'm relatively new to node and socketio. What I'm trying to achieve is authenticate users using passport and send total number logged user count to the view. Such that if a logged in user can see the total number of current-logged-in users and if a user logs that the count decreases or increases when someone else log-ins respectively.
Using passport.socketio to access authenticated passport user information from a socket.io connection.
In the callback, I'm storing the username in a mongoose collection, and accordingly on the logout removing the user from the collection. I get a count of the number of users in the model which I need to pass and bind to the view. Jade being the template engine. Below is how my onAuthorizeSuccess callback looks like where I try to pass the count to home.jade.
function onAuthorizeSuccess(data, accept) {
var username = data.user.username;
var User = mongoose.model('loggedusers', userSchema);
var user = new User({
username: username
});
user.save(function (err, data) {
if (err) console.log(err);
else {
console.log('Saved : ', data);
}
User.count({}, function (err, c) {
console.log('Count is ' + c);
app.get('/', function (req, res) {
res.render('home', {
count: {
countData: c
}
});
});
});
});
console.log('successful connection to socket.io ');
accept(); //Let the user through
}
And in the jade view I try to set it using
li Logged Users ---> #{countData.c}
But, countData is undefined in the view.
How should I go about rendering a dynamic value from the server to the view in jade?
Any assistance much appreciated.
Thanks,
Arnab
Your variable is wrong you should use instead #{count} with:
res.render('home', {count: c});
Figured this out.
Made a function to server content over a socket, that the control helps updating on the front-end
module.exports = function (socket) {
setInterval(function () {
var UserSchema = require('mongoose').model('loggedusers');
UserSchema.count({}, function(err, c){
console.log('Count is ' + c);
socket.emit('send:count', {
count: c
});
});
}, 1000);
};
And the angular controller
angular.module('myApp.controllers', []).
controller('AppCtrl', function ($scope, socket) {
socket.on('send:count', function (data) {
$scope.count = data.count;
});
});
and in jade {{count}} and add div.container(ng-controller='AppCtrl') should give the updated count on the front-end.
Arnab
I have a node.js application that uses a mongodb database that I've created. Within it, I have a simple collection named comments with the contents { "author": "me", "comment": "this is a comment" } when I call db.comments.find({}).
However, when I attempt to access this collection for display within a jade view I have, it times out after an incrediable amount of time. Console.log for the error object shows it's either a MongoError or connection was destroyed by application. The question I have is why this is happening? I have no errant while loops and connection parameteres seem to check out. Here's what I have to connect with, stored in app.js
var app = express();
var mongodb = require('mongodb'),
serverdb = new mongodb.Server('127.0.0.1', 27017, {}),
db = new mongodb.Db('acl', serverdb, {safe:true});
app.use(function(req,res,next){
req.db = db;
next();
});
and the code I have in the middleware file, stored as a js file in /routes
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
var db = req.db;
var collection = db.collection('comments');
collection.find().toArray(function(err, docs) {
console.log("Printing docs from Array");
if (err) {
console.log(err);
} else {
console.log(docs);
}
});
db.close();
});
module.exports = router;
Like #legalize said, its best to get a mongo connection pool going instead of opening and closing the connection on every request. Perhaps something like this SO answer
As far as why you are getting errors, its probably because your db.close() needs to be in the collection.find().toArray() callback because otherwise it'll start closing the connection before the query even happens.
Lastly, you need to render the template somewhere so the response gets sent back to the client.
Putting it all together, you probably want something like this:
router.get('/', function(req, res) {
var db = req.db;
var collection = db.collection('comments');
collection.find().toArray(function(err, docs) {
console.log("Printing docs from Array");
db.close();
if (err) {
console.log(err);
} else {
console.log(docs);
res.render( 'yourJadeTemplate', { docs : docs } );
}
});
});
(but you really don't want to be closing the connection for every request, especially because you aren't opening it for every request)
Oddly enough replacing this code
var mongodb = require('mongodb'),
serverdb = new mongodb.Server('127.0.0.1', 27017, {}),
db = new mongodb.Db('acl', serverdb, {safe:true});
with this
var db = require("mongojs").connect("localhost:27017/acl", ["comments"]);
made all the difference. No more timeouts. A bit of tweeking to get it to return data.
Every time I update the database with a new menu item, I'm trying to get the routing to update with one more route. Here's my sad little ugly attempt:
Here in app.js, I check the menu database and shazaam...routes are made on the fly at startup. Cool!:
// in app.js //
var attachDB = function(req, res, next) {
req.contentdb = db.content;
req.menudb = db.menu;
req.app = app; // this is the express() app itself
req.page = PageController;
next();
};
db.menu.find({}, function (err, menuitems){
for(var i=0; record = menuitems[i]; i++) {
var menuitem = record.menuitem;
app.all('/' + menuitem, attachDB, function(req, res, next) {
console.log('req from app all route: ',req)
PageController.run(menuitem, req, res, next);
});
}
http.createServer(app).listen(config.port, function() {
console.log(
'\nExpress server listening on port ' + config.port
);
});
});
Not real elegant but it's a proof of concept. Now here's the problem: When I save a new menu item in my Admin.js file, the database get's updated, the router seems to get updated but something about the request just blows up after clicking on a menu link with a dynamically created route
Many things in the request seem to be missing and I feel like there is something fundamental I don't understand about routing, callbacks or perhaps this is just the wrong solution. Here's what the function responsible for creating a new menu item and creating a new route in my Admin.js file looks like:
// in Admin.js //
menuItem: function(req, res, callback) {
var returnMenuForm = function() {
res.render('admin-menuitem', {}, function(err, html) {
callback(html);
});
};
var reqMenudb = req.menudb,
reqContentdb = req.contentdb,
reqApp = req.app,
reqPage = req.page;
if(req.body && req.body.menuitemsubmitted && req.body.menuitemsubmitted === 'yes') {
var data = { menuitem: req.body.menuitem };
menuModel.insert( data, function(err) {
if (err) {
console.log('Whoa there...',err.message);
returnMenuForm();
} else {
// data is inserted....great. PROBLEM...the routes have not been updated!!! Attempt that mimics what I do in app.js here...
reqApp.all('/' + data.menuitem, function(req, res, next) {
// the 2 db references below are set with the right values here
req.contentdb = reqContentdb;
req.menudb = reqMenudb;
next();
}, function(req, res, next) {
reqPage.run(data.menuitem, req, res, next);
});
returnMenuForm();
}
});
} else {
returnMenuForm();
}
},
Saving the data in the admin section works fine. If you console log app.routes, it even shows a new route which is pretty cool. However after refreshing the page and clicking the link where the new route should be working, I get an undefined error.
The admin passes data to my Page controller:
// in PageController.js //
module.exports = BaseController.extend({
name: "Page",
content: null,
run: function(type, req, res, next) {
model.setDB(req.contentdb); /* <-- problem here, req.contentdb is undefined which causes me problems when talking to the Page model */
var self = this;
this.getContent(type, function() {
var v = new View(res, 'inner');
self.navMenu(req, res, function(navMenuMarkup){
self.content.menunav = navMenuMarkup;
v.render(self.content);
});
});
},
getContent: function(type, callback) {
var self = this;
this.content = {}
model.getlist(function(records) {
if(records.length > 0) {
self.content = records[0];
}
callback();
}, { type: type });
}
Lastly, the point of error is here in the model
// in Model.js //
module.exports = function() {
return {
setDB: function(db) {
this.db = db;
},
getlist: function(callback, query) {
this.db.find(query || {}, function (err, doc) { callback(doc) });
},
And here at last, the 'this' in the getlist method above is undefined and causes the page to bomb out.
If I restart the server, everything works again due to my dynamic loader in app.js. But isn't there some way to reload the routes after a database is updated?? My technique here does not work and it's ugly to be passing the main app over to a controller as I'm doing here.
I would suggest two changes:
Move this menu attachment thing to a separate module.
While you're at it, do some caching.
Proof of concept menu db function, made async with setTimeout, you'll replace it with actuall db calls.
// menuitems is cached here in this module. You can make an initial load from db instead.
var menuitems = [];
// getting them is simple, always just get the current array. We'll use that.
var getMenuItems = function() {
return menuitems;
}
// this executes when we have already inserted - calls the callback
var addMenuItemHandler = function(newItem, callback) {
// validate that it's not empty or that it does not match any of the existing ones
menuitems.push(newItem);
// remember, push item to local array only after it's added to db without errors
callback();
}
// this one accepts a request to add a new menuitem
var addMenuItem = function(req, res) {
var newItem = req.query.newitem;
// it will do db insert, or setTimeout in my case
setTimeout(function(newItem){
// we also close our request in a callback
addMenuItemHandler(newItem, function(){
res.end('Added.');
});
}, 2000);
};
module.exports = {
addMenuItem: addMenuItem,
getMenuItems: getMenuItems
}
So now you have a module menuhandler.js. Let's construct it and use it in our app.
var menuHandler = require('./menuhandler');
var app = express();
// config, insert middleware etc here
// first, capture your static routes - the ones before the dynamic ones.
app.get('/addmenuitem', menuHandler.addMenuItem);
app.get('/someotherstaticroute', function(req, res) {
var menu = menuHandler.getMenuItems();
res.render('someview', {menu: menu});
});
// now capture everything in your menus.
app.get('/:routename', function(req, res){
// get current items and check if requested route is in there.
var menuitems = menuHandler.getMenuItems();
if(menuitems.indexOf(req.params.routename) !== -1) {
res.render('myview', {menu: menuitems});
} else {
// if we missed the route, render some default page or whatever.
}
});
app.get('/', function(req, res) {
// ...
});
Now you don't go to db if there were no new updates (since menuitems array is always up to date) so your initial view is rendered faster (for that 1 db call, anyway).
Edit: oh, I just now saw your Model.js. The problem there is that this refers to the object you have returned:
{
setDB: function(db) {
this.db = db;
},
getlist: function(callback, query) {
this.db.find(query || {}, function (err, doc) { callback(doc) });
}
}
So, no db by default. And since you attach something to the app in the initial pageload, you do get something.
But in your current update function, you attach stuff to the new app (reqApp = req.app), so now you're not talking to the original app, but another instance of it. And I think that your subsequent requests (after the update) get the scope all mixed up so lose the touch with the actual latest data.
In your code when you start your server it reads from the menu db and creates your routes. When your menu changes, you do not re-read from db again.
I suggest you do something like the following
app.all('*', function(req, res) {
//read from your menu db and do the the route management yourself
});