Node.js Express JSON search functionality - json

Using MEAN stack to create search functionality using JSON data. As shown below by connecting to mongo DB and pushing everything to the data array.
app.get('/all/', function(req, res) {
var data = [];
mongodb.MongoClient.connect(url, function(err, db) {
var position = db.collection('Namers').find();
position.forEach(function(doc, err) {
data.push(doc);
}, function() {
db.close();
res.json(data);
});
});
});
I want to do a parameter search like:
app.get('all/:search)
In order to filter the JSON information corresponding to either the Name or Codes that is in my JSON file. Which an example can be seen below:
[{"Name":"Bob", "Code":"23234"},{"Name":"Tim", "Code":"24924"}]
How would I go about achieving this using express (Node.js)?
edit: (complete code)
app.get('/all/', function(req, res) {
var data = [];
mongodb.MongoClient.connect(url, function(err, db) {
var position = db.collection('Modules').find();
position.forEach(function(doc, err) {
data.push(doc);
}, function() {
db.close();
var filtered = data.filter(function(item){
var result = false;
Object.keys(item).map(function(key){
if (item[key] == req.params.search){
result = true;
}
})
return result;
});
res.json(filtered);
});
});
});
app.get('all/:search', function(req, res) {
});

app.get('/all/', function(req, res) {
var data = [];
mongodb.MongoClient.connect(url, function(err, db) {
var position = db.collection('Modules').find();
position.forEach(function(doc, err) {
data.push(doc);
}, function() {
db.close();
res.json(data);
});
});
});
app.get('all/:search', function(req, res) {
var data = [];
mongodb.MongoClient.connect(url, function(err, db) {
var position = db.collection('Modules').find();
position.forEach(function(doc, err) {
data.push(doc);
}, function() {
db.close();
var filtered = data.filter(function(item){
var result = false;
Object.keys(item).map(function(key){
if (item[key] == req.params.search){
result = true;
}
})
return result;
});
res.json(filtered);
});
});
});

Related

Convert directory structure in the filesystem to Json object

I Know how to convert the directory structure into JSON object, from here
But I want all the files in an array, and the folders in object with the object key as the folder name. I have been trying for a long time but just cannot get it done.
Thi is what I have tried so far:
var diretoryTreeToObj = function (dir, done) {
var results = {};
var _contents = [];
fs.readdir(dir, function (err, list) {
if (err) {
return done(err);
}
var pending = list.length;
if (!pending) {
return done(null, {name: path.basename(dir), type: 'folder', children: results});
}
list.forEach(function (file, index) {
file = path.resolve(dir, file);
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
diretoryTreeToObj(file, function (err, res) {
results[path.basename(file)] = {
name: path.basename(file),
type: 'folder',
path: path.dirname(file),
_contents: [res]
};
if (!--pending) {
done(null, results);
}
});
} else {
results['_contents'] = [{
name: path.basename(file),
path: file
}];
if (!--pending) {
done(null, results);
}
}
});
});
});
};
Thanks in advance. :)
Finally I figured out the solution, here it is if anybody needs it:
var diretoryTreeToObj = function (dir, done) {
var results = {};
var _contents = [];
var files = [];
fs.readdir(dir, function (err, list) {
if (err) {
return done(err);
}
var pending = list.length;
if (!pending) {
return done(null, {name: path.basename(dir), type: 'folder', children: results});
}
list.forEach(function (file, index) {
file = path.resolve(dir, file);
fs.stat(file, function (err, stat) {
results['_contents'] = files;
if (stat && stat.isDirectory()) {
diretoryTreeToObj(file, function (err, res) {
results[path.basename(file)] = res;
if (!--pending) {
done(null, results);
}
});
} else {
files.push({
name: path.basename(file),
path: file
});
results['_contents'] = files;
if (!--pending) {
done(null, results);
}
}
});
});
});
};

error: POST http://localhost:3000/api/createProductCategory 500 (Internal Server Error)

I am trying to insert data into a table using POST method for that I have a angular service function
angular.module("productCategoryModule")
.factory("productCategoryService",productCategoryService);
productCategoryService.$inject = ['$http'];
function productCategoryService($http){
return {
createProductCategory:function(productCategory){
console.log("createProductCategory in service called",productCategory);
return $http.post('/api/createProductCategory',
{
categoryName:productCategory.categoryName,
details:productCategory.categoryDetails
}
);
},
getAllProductCategories:function(){
return $http.get('/api/getAllProductCategory');
}
}
}
and at server side I have
function productCategoryRouteConfig(app){
this.app = app;
this.routeTable = [];
this.init();
}
productCategoryRouteConfig.prototype.init = function(){
var self = this;
this.addRoutes();
this.processRoutes();
}
productCategoryRouteConfig.prototype.processRoutes = function(){
var self = this;
self.routeTable.forEach(function(route){
if(route.requestType == 'get'){
//console.log("requestType",route.requestType)
self.app.get(route.requestUrl,route.callbackFunction);
} else if(route.requestType == 'post'){
//console.log("requestType",route.requestType);
self.app.post(route.requestUrl,route.callbackFunction);
} else if(route.requestType == 'delete'){
}
});
}
productCategoryRouteConfig.prototype.addRoutes = function(){
var self = this;
self.routeTable.push({
requestType: 'get',
requestUrl: '/createProductCategory',
callbackFunction: function(req, res){
res.render('createProductCategory',{title:'Create Product Category'});
}
});
self.routeTable.push({
requestType: 'post',
requestUrl: '/api/createProductCategory',
callbackFunction: function(req, res){
console.log("Post called");
//res.render('createProductCategory');
console.log("req.body",req.body);
var productCategoryDb = require('../database/productCategoryDb');
// console.log("productCategoryDb post",productCategoryDb);
// console.log("hello from createProductCategory post");
// console.log("req.body",req.body);
// productCategoryDb.productCategoryDb.createProductCategory(req.body, function(status){
// if(status)
// res.json(status);
// console.log(status);
// });
}
});
self.routeTable.push({
requestType: 'get',
requestUrl: '/viewProductCategory',
callbackFunction: function(req, res){
res.render('viewProductCategory',{title:'View Product Category'});
}
});
self.routeTable.push({
requestType: 'get',
requestUrl: '/api/getAllProductCategory',
callbackFunction: function(req, res){
console.log("hello from getAllProductCategory");
var productCategoryDb = require('../database/productCategoryDb');
console.log("productCategoryDb",productCategoryDb);
// productCategoryDb.productCategoryDb.getAllProductCategories(
// function (productCategories){
// console.log("productCategories",productCategories);
// res.json({productCategories : productCategories});
// }
// );
}
});
}
module.exports = productCategoryRouteConfig;
when I click on the button on client side I get this error
POST http://localhost:3000/api/createProductCategory 500 (Internal Server Error)
I am using Node express mysql and angular.
There are three files in me database folder.
1.connectionString.js
var mysqlConnectionString = {
connectionString:{
host:'localhost',
user:'root',
password:'root',
database:'vidzy'
}
}
//module.exports = mysqlConnectionString;
exports.mysqlConnectionString = mysqlConnectionString;
2.connection.js
var mysql = require('mysql');
var mysqlConnectionString = require('/home/ep-3/node-express/yt_tutorial/database/connectionString.js');
var connectionStringProvider = {
getSqlConnection:function(){
var connection = mysql.createConnection(mysqlConnectionString.mysqlConnectionString.connectionString);
connection.connect(function(err){
if(err){
throw err;
} else{
console.log("connection was successful");
}
});
return connection;
},
closeSqlConnection:function(currentConnection){
currentConnection.end(function(err){
if(err){
throw err;
} else{
console.log("Disconnected");
}
})
}
}
exports.connectionStringProvider = connectionStringProvider;
3.productCategoryDb.js
var connectionProvider = require('/home/ep-3/node-express/yt_tutorial/database/connection.js');
var productCategoryDb = {
createProductCategory : function(productCategory, onSuccessful){
var insertStatement = 'INSERT INTO productcategory SET?';
var category = {
categoryName : productCategory.categoryName,
Details : productCategory.details,
isValid : productCategory.isValid,
CreatedDate : new Date()
}
var connection = connectionProvider.connectionStringProvider.getSqlConnection();
if(connection){
connection.query(insertStatement, category, function(err, result){
if(err){
console.log(err);
}
onSuccessful({status : 'Successful'});
console.log(result);
});
connectionProvider.connectionStringProvider.closeSqlConnection(connection);
}
},
getAllProductCategory : function(callback){
var connection = connectionProvider.connectionStringProvider.getSqlConnection();
var selectStatement = 'SELECT * FROM productcategory';
if(connection){
connection.query(selectStatement, function(err, rows, fields){
if(err){ through err; }
console.log(rows);
callback(rows);
});
}
}
}
exports.productCategoryDb = productCategoryDb;
Are you sure you have included the module body-parser.
It seems the code you posted here to be the same as the tutorial I have been following.
Your code seems to be fine except I don't know what your code in app.js looks like.
I have verified that I get the console response for req.body as undefined when I comment out the module body-parser.
I got the same error but in my case there was a node-modules function that increments id for each POST I made and i forgot to add id in my DTO..and now it's working; maybe it's not your case since u are using mysql but I'll post the answer anyway, maybe some1 will resolve his error thank to this help

Getting the values of a returned JSON object in angular js

I am new to angular and node.js.
Basically, I have a table of items and in each row, there is a button click.
If I click the button which is edit, the details should appear in its corresponding textbox for editing.
router.route('/contacts/getone/:pk')
.get(function(req, res, next){
pg.connect(connectionString, function(err, client, done) {
//var id = {pk:req.body.pk};
var contacts = [];
console.log("get1");
console.log(req.params.pk);
var query = client.query("select * from contacts where pk=($1)", [req.params.pk]);
query.on('row', function(row) {
contacts.push(row);
});
query.on('end', function() {
done();
return res.json(contacts);
});
});
});
And this is my angular controller code.
$scope.editContact = function(id) {
contact.getone(id)
.success(function(data) {
$scope.contactlist = data;
//console.log("geone");
//document.getElementById("fname").value = $scope.contactlist.firstname;
//console.log("getall");
//console.log($scope.contactlist.firstname);
//console.log(data["firstname"]);
$scope.formData.firstname = $scope.contactlist.firstname;
$scope.formData.lastname = $scope.contactlist.lastname;
$scope.formData.address = $scope.contactlist.address;
$scope.formData.contact = $scope.contactlist.contact;
// $scope.loading = false;
});
};
Can anyone tell me how to access the values in data which is returned by my node.js code? The code I have in my angular returns undefined values like $scope.contactlist.firstname is undefined.
This is the image of when I do console.log(data);
#Rayin Dabre
----This is my the code of my controller.
angular.module('ContactsControl', [])
.controller('contactController', ['$scope', '$http', 'contact', function($scope, $http, contact) {
$scope.formData = {};
contact.get()
.success(function(data) {
console.log("getall");
$scope.contactlist = data;
// $scope.loading = false;
});
$scope.editContact = function(id) {
contact.getone(id)
.success(function(data) {
$scope.contactlist = data;
//console.log("geone");
//document.getElementById("fname").value = $scope.contactlist.firstname;
console.log(data);
//console.log($scope.contactlist.firstname);
//console.log(data["firstname"]);
$scope.formData.firstname = $scope.contactlist.firstname;
$scope.formData.lastname = $scope.contactlist.lastname;
$scope.formData.address = $scope.contactlist.address;
$scope.formData.contact = $scope.contactlist.contact;
// $scope.loading = false;
});
};
You have to define formData as object({}) or else $scope.formData will be undefined and you can not set property of undefined
Data being received from the api is not an object but an array. You need to return the data once you get the row
Server side
router.route('/contacts/getone/:pk')
.get(function(req, res, next) {
pg.connect(connectionString, function(err, client, done) {
var query = client.query("select * from contacts where pk=($1)", [req.params.pk]);
query.on('row', function(row) {
return res.json(row);
});
});
});
Client side
var data = {
$$hashKey: "008",
contact_num: 123,
email: "a#gmail.com",
firstname: "boy",
lastname: "kigwa",
pk: 2
};
$scope.formData = {};
$scope.contactlist = data;
$scope.formData.firstname = $scope.contactlist.firstname;
$scope.formData.lastname = $scope.contactlist.lastname;
$scope.formData.address = $scope.contactlist.address;
$scope.formData.contact = $scope.contactlist.contact;
alert(JSON.stringify($scope.formData));

Data not getting saved in the MongoDB from node.js

I want to create the rest api using node.js and mongodb
I am entering all the details and trying it to store it in the mongodb database.
// call the packages we need
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var morgan = require('morgan');
// configure app
app.use(morgan('dev')); // log requests to the console
// configure body parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
var mongoose = require('mongoose');
// mongoose.connect('mongodb://node:node#novus.modulusmongo.net:27017/Iganiq8o'); // connect to our database
mongoose.connect('mongodb://localhost:27017');
var Bear = require('./app/models/bear');
// create our router
var router = express.Router();
// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
console.log('Something is happening.');
next();
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
// on routes that end in /bears
// ----------------------------------------------------
router.route('/bears')
// create a bear (accessed at POST http://localhost:8080/bears)
.post(function(req, res) {
var bear = new Bear(); // create a new instance of the Bear model
bear.name = req.body.name; // set the bears name (comes from the request)
bear.email= req.body.email; // set the bears email(comes from the request)
bear.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Bear created!' });
});
})
// get all the bears (accessed at GET http://localhost:8080/api/bears)
.get(function(req, res) {
Bear.find(function(err, bears) {
if (err)
res.send(err);
res.json(bears);
});
});
// on routes that end in /bears/:bear_id
// ----------------------------------------------------
router.route('/bears/:bear_id')
// get the bear with that id
.get(function(req, res) {
Bear.findById(req.params.bear_id, function(err, bear) {
if (err)
res.send(err);
res.json(bear);
});
})
// update the bear with this id
.put(function(req, res) {
Bear.findById(req.params.bear_id, function(err, bear) {
if (err)
res.send(err);
bear.name = req.body.name;
bear.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Bear updated!' });
});
});
})
// delete the bear with this id
.delete(function(req, res) {
Bear.remove({
_id: req.params.bear_id
}, function(err, bear) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
// REGISTER OUR ROUTES -------------------------------
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);
The Model is given below:-
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var BearSchema = new Schema({
name: String,
email: String
});
module.exports = mongoose.model('Bear', BearSchema);
I am trying it to save the name and the email in the mongodb database but only _id is created instead of name, email.
Here is the result:-
[
{
"_id": "567f1f92db24304013000001",
"__v": 0
},
{
"_id": "567f2765db24304013000002",
"__v": 0
}
]
Can anybody tell me why the data are not getting saved in the database.
Please kindly help.
Thanks in Advance.
I think your POST request is not good, so I made this simple script to check it out:
var XHR = (function() {
var _xhr = (function() {
try {
return new(this.XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
} catch (e) {}
})();
return function(method, url, params, callback) {
_xhr.onreadystatechange = function() {
if (_xhr.readyState == 4) {
var _response;
try {
_response = JSON.parse(_xhr.response);
} catch (e) {
_response = _xhr.responseText;
}
if (_xhr.status != 200) {
// catch an error
console.error('error', response);
} else {
if (callback) {
callback(_response);
} else {
// deal with it
}
}
}
}
if (!params) {
params = JSON.stringify({});
} else {
params = JSON.stringify(params);
}
_xhr.open(method, url, true);
// just json in this case
_xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8");
_xhr.send(params);
};
})();
fire it up in browser's console, like this
XHR('POST','api/bears', { name:'yogi', email:'yogi#bears.com'}, function(){ console.log(arguments) });
and your record will be saved.
{ "_id" : ObjectId("567e875d068748ee5effb6e0"), "email" : "yogi#bears.com" "name" : "yogi", "__v" : 0 }
Long story short - your code is okay, your POST is not.

mysql million rows express.js slow response

I am using node.js v0.10.33 with node-mysql 2.5.4 and express.js 4.10.2 and fast-csv 0.6.0
Any following requests made to the server slows down after a request to "query 9000000 rows and download it as csv". Is there a better way to avoid the slow response?
/**
* problem is why the server response slows down on a large request mande to mysqlrows_to_csv which queries 9000000 rows and saves as csv
*/
(function () {
'use strict';
var async = require('async');
var csv = require("fast-csv");
var fs = require('fs');
var mysql = require('mysql');
//var express = require('express');
//var expRouter = express.Router(); // Express Router
expRouter.get('/mysqlrows_to_csv/', function(req, res, next) {
mysqlrows_to_csv(function(err) {
console.log('use socket to emit a notification to client saying csvfile is ready');
});
res.json({'mysqlrows_to_csv': true});
return res.end();
});
expRouter.get('/some_other_queries/', function(req, res, next) {
res.json({'some_other_queries': true});
return res.end();
});
function mysqlrows_to_csv(callback) {
var csvStream;
var connProp = {
host : 'host',
user : 'user',
password : 'password',
database : 'database'
};
var I = 0;
async.waterfall([
function(cb) {
var connection = mysql.createConnection(connProp);
connection.connect(function(err) {
if (err) return callback(err);
csvStream = csv.createWriteStream({headers: true});
var writableStream = fs.createWriteStream("dump_mysql_to_csv.csv");
writableStream.on("error", function(err){
return callback(err);
});
writableStream.on("finish", function(){
console.log("DONE!");
return callback(null);
});
if (csvStream) csvStream.pipe(writableStream);
return cb(err, connection);
});
},
function(connection, cb) {
var qry = 'select * from table'; // this has aleast 9000000 rows
var query = connection.query(qry);
query
.on('error', function(err){
return cb(err);
})
.on('result', function(row) {
connection.pause();
I = I + 1;
console.log('row no.', I);
if (csvStream) csvStream.write(row);
connection.resume();
})
.on('end', function() {
console.log('stream-ended')
if (csvStream) csvStream.end();
return cb(null, connection);
});
},
function(connection, cb) {
connection.end(function(err) {
if (err) {
console.log('Error while disconnecting mysql', err);
}
console.log('The connection is terminated now');
console.log('final conn state:', connection.state);
});
}
]);
}
}());