How to insert json output into mysql with node js - javascript

var express = require('express');
var fs = require('fs');
var mysql = require('mysql');
var request = require('request');
var cheerio = require('cheerio');
var bodyParser = require('body-parser');
var app = express();
var output;
app.use(bodyParser.json())
app.get('/scrape', function(req, res){
url = 'https://raitalumni.dypatil.edu/events/?tag=live';
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
var json = {
title : [],
date : [],
month : [],
venue : [],
link : []
};
output = {
events : []
};
$('p.event_name').each(function(){
json.title.push($(this).text());
});
$('p.calendar_date').each(function(){
json.date.push($(this).text());
});
$('span.calendar_month').each(function(){
json.month.push($(this).text());
});
//var fulldate = $('p.calendar_date').concat($('p.calendar_day')).text();
//console.log('all records: ' + fulldate);
$('p.event_venue').each(function(){
json.venue.push($(this).text());
});
// var title = $('p.event_name').each(function(){$(this).text()});
for(var i=0; i<json.title.length; i++){
output.events[i] = {
title : json.title[i],
date : json.date[i],
month : json.month[i],
venue : json.venue[i],
link : url
}
}
var connection = mysql.createConnection({
host: '127.0.0.1',
port: 3306,
user: 'root',
password: '',
database: 'raithub'
});
connection.connect(function(error){
if(!!error){
console.log('Error');
}else{
console.log('Connected to the database!');
}
});
var scrape = JSON.stringify(output, null, 4);
console.log(scrape);
var query = connection.query("INSERT INTO scrapped ('title','date','month','venue','link') VALUES ('" + output.title + "', '" + output.date + "', '" + output.month + "', '" + output.venue + "', '" + output.link + "');", scrape, function(err, result) {
if(err) throw err;
console.log('data inserted');
});
fs.writeFile('output4.json', JSON.stringify(output, null, 4), function(err){
console.log('File successfully written! - Check your project directory for the output.json file');
})
res.send('Check your console!')
}
else {
console.log("Network Error, please try again later")
}
})
})
app.listen('8000')
console.log('Server running on port 8081');
exports = module.exports = app;
Where am I going wrong?
Getting this error,
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''title','date','month','venue','li
nk') VALUES ('undefined', 'undefined', 'undefi' at line 1 error

The INSERT statement should look this:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
where the column names should not be in quotes. Your specific statement would like this:
var query = connection.query("INSERT INTO scrapped (title,date,month,venue,link) VALUES ('" + output.title + "', '" + output.date + "', '" + output.month + "', '" + output.venue + "', '" + output.link + "');", scrape, function(err, result) {
if(err) throw err;
console.log('data inserted');
});
See this for the right syntax

There is a JSON datatype in MySQL.
mysql> CREATE TABLE t1 (jdoc JSON);
Query OK, 0 rows affected (0.20 sec)
mysql> INSERT INTO t1 VALUES('{"key1": "value1", "key2": "value2"}');
Query OK, 1 row affected (0.01 sec)
This would be especially helpful if you had a very large json object and didn't want to create a huge INSERT statement. It would also be useful if you are unsure of the exact data that will be coming in via json but you want to capture all of it. I'm currently working on a project where we occasionally add and remove items from the user input fields and this is very helpful so that I'm not constantly having to ALTER tables and edit other statements in MySQL.
More information can be found here: https://dev.mysql.com/doc/refman/5.7/en/json.html

const queryString = `insert into table_name(meta) values ('${JSON.stringify(meta)}');`;

Related

Nodejs: I get "FSReqWrap.readFileAfterClose"-error when I try to read a json file

I am trying to append objects to JSON files.
For example, I have JSON files like:
{
"_1": {
"watch" : "undefined"
},
"_2": {
"watch" : "undefined"
}
}
and that is the part of my app.js that doesn't work:
app.post('/add2', (req, res) => {
storeAt = path.join(__dirname + '/json/' + req.body.addTo + '.json');
id = req.body.id;
i = req.body.i;
fs.readFile(storeAt, (err, data) => {
console.log("data: " + data);
var temp = JSON.parse(data);
temp = temp.substring(0, temp.length - 2);
temp += ',\n\t"_' + i + '": {\n\t\t"watch" : "' + id + '"\n\t}\n}';
fs.writeFile(storeAt, JSON.stringify(temp));
});
So basically I want to read the file and save it in a variable, then delete the last to characters, add something new to it and save it.
The console.log("data : + data); works just fine; the content of the JSON file is displayed on the console. But the problem is in the line with the .substring. I get this error in the console:
TypeError: temp.substring is not a function
at fs.readFile (C:\...\app.js:56:21)
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:442:3)
I couldn't find any help on the internet and I don't really understand the TypeError. Can anyone help?

Display MySQL data using Node.js

I am making a small script where I enter form data into a database table when I press submit.
What I want to do is to show the added data on the next page i.e. app.post for /form
at the moment I am getting only a employee ......with salary ..... added
But I want to display the whole row of the data I just added.
Now, how do I do that?
here's the code for server.js which i use to run the app.
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var app = express();
var mysql = require('mysql');
var connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'',
database:'employees'
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/', function (req,res){
res.sendFile(path.join(__dirname,'index.html'));
});
app.get('/add-employee.html',function(req,res){
res.sendFile(path.join(__dirname,'/add-employee.html'));
});
app.post('/add-employee.html',function(req,res){
res.status(200).send();
console.log('employee '+req.body.name + ' added');
});
connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
app.post('/form',function (req,res){
//employee details from add-employee.html page
var name=req.body.name;
var designation = req.body.designation;
var pan = req.body.pan;
var aadhar = req.body.aadhar;
var bank = req.body.bank;
var salary = req.body.salary;
var allowance = req.body.allowance;
var esi = req.body.esi;
var uan = req.body.uan;
var details = {name:name,designation:designation,pan:pan,aadhar:aadhar,
bank:bank,salary:salary,allowance:allowance,esi:esi,uan:uan};
var query = connection.query('INSERT INTO employee_details SET ?',details,function(err,result){
if(err){
console.log(err);
}
console.log(query.sql);
});
connection.query('SELECT * FROM employee_details',function(err,rows){
if(err) throw err;
console.log('Data received');
console.log(rows);
});
res.status(200).send('employee ' + name + 'with salary of '+salary+ ' added');
});
app.get('/add-company.html',function(req,res){
res.sendFile(path.join(__dirname,'/add-company.html'));
});
app.get('/style.css',function(req,res){
res.sendFile(path.join(__dirname,'/style.css'));
});
app.get('/main.js',function(req,res){
res.sendFile(path.join(__dirname,'/main.js'));
});
app.get('/cmain.js',function(req,res){
res.sendFile(path.join(__dirname,'/cmain.js'));
});
var port=8080;
app.listen(8080,function(req,res){
console.log(`Payroll app listening on port ${port}!` );
});
I am able to see the rows from the select query in the console.
But the problem Is I dont know how to display that on the page itself.
how do I do that?
connection.query('INSERT INTO employee_details SET ?',details,function(err,result){
if(err){
console.log(err);
}
else{
res.status(200).send(result);
}
});
Remove the below part this will send a result from where you called this api and you can display that result
You need to select after insert.
Pay attention to the queries are async.
var query = connection.query('INSERT INTO employee_details SET ?',details,function(err,result){
if(err){
console.log(err);
}
console.log(query.sql);
connection.query('SELECT * FROM employee_details',function(err,rows){
if(err) throw err;
console.log('Data received');
console.log(rows);
res.status(200).send('employee ' + name + 'with salary of '+salary+ ' added');
});
});

Javascript - accessing password in ssh2 connection

I have a class which is being used to connect to a device. I have made in instance of the class in my application
app.js
myConn = new myConnection();
myConnection.js
function myConnection(){
this.settings = {
host: '192.168.225.195',
port: 22,
username: 'sysadmin',
password: 'pass'
};
}
I have a function within said class that executes a command on the remote device but that requires a password. When this happens stderr.on is executued and I send the password and a newline char.
myConnection.prototype.installPatch = function(callback){
this.conn.exec('sudo -S bash /tmp/update.sh', function(err, stream){
var standardMsgs = '';
var errorMsgs = '';
if(err) throw err;
stream.on('close', function(code, signal) {
callback(standardMsgs, errorMsgs);
}).on('data', function(data) {
standardMsgs += "<br>" + data;
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
errorMsgs += data;
console.log('STDERR: ' + data);
stream.write(myConn.conn.config.password + '\n');
});
});
}
While this works I am not a fan of accessing the password with
stream.write(myConn.conn.config.password + '\n');
since a change to the name "myConn" in app.js would required the same change in the "installPatch" function.
I had intended to use
stream.write(this.settings.password + '\n');
Do I have any other options that will allow me to retrieve the password from within the myConnection class? I hope I am just overlooking the obvious.
Ok, so I believe it was starring me in the face.
Change
stream.write(myConn.conn.config.password + '\n');
to
stream.write(stream._client.config.password + '\n');

Node JS: Convert retrieved data from DB as JSON to Plain text

In the following code, data are retrieved from a database into JSON. What I would like to do is to display each single data as a plain text:
var http = require("http");
var mysql = require('mysql');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(express.static('public'));
app.get('/Search.html', function (req, res) {
res.sendFile( __dirname + "/" + "Search.html" );
})
var connection = mysql.createConnection(
{
host : 'localhost',
user : 'root',
password : 'passpass',
database : 'SocialQuery',
}
);
connection.connect();
app.post('/process_post', urlencodedParser, function (req, res) {
// Prepare output in JSON format
response = {
SearchType:req.body.SearchTypes,
Term:req.body.term
};
//var vas = JSON.stringify(response);
var search = req.body.SearchTypes;
var term = req.body.term;
var queryString;
if(search == 'Author')
{
queryString = 'Select Label,TDate from Tweet where AuthorID IN (select ID from Author where Lable = ?)';
}
else if(search == 'Mention')
{
queryString = 'select Tweet.Label, Tweet.TDate, Author.Lable from Tweet, Author where Tweet.ID IN (select TweetID from TweetMention where MentionID IN (select ID from Mention where Label = ?)) AND Author.ID = Tweet.AuthorID'
}
var query = connection.query(queryString, [term], function(err, rows) {
console.log(rows);
var tweet = JSON.stringify(rows);
res.write("Author: " + tweet.Lable);
res.write("Date: " + tweet.TDate);
res.end();
});
console.log(query.sql);
})
//}).listen(8081);
http.createServer(app).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');
When I print the data using res.write(JSON.stringify(rows)); I got the following:
[{"Label":"lest play hero","TDate":"2016-03-12T00:00:00.000Z","Lable":"esti_jony"},{"Label":"u r the best ! Ill always keep","TDate":"2016-03-08T00:00:00.000Z","Lable":"adam03cooper03"}]
but when I run the code above, I got:
Author: undefined Date: undefined
What I understood is the problem because two rows of data have been retrieved and I do not know how to let it display each author (Lable) and each date (TDate).
You're converting a javascript object to a JSON string and then trying to access the javascript object's properties on the string. That won't work. You want to use the javascript object instead, but since there is more than one row, you will have to either choose which one you want to respond with or write all rows or whatever your use case calls for.
For example:
var query = connection.query(queryString, [term], function(err, rows) {
if (err) throw err;
for (var i = 0; i < rows.length; ++i) {
var tweet = rows[i];
res.write("\nAuthor: " + tweet.Lable);
res.write("\nDate: " + tweet.TDate);
if (i + 1 < rows.length)
res.write('\n');
}
res.end();
});

Refreshing contents of an Express NodeJS API

I have written an API that queries a MySQL database and outputs the corresponding results visiting an url. This is the code:
//server.js
var express = require('express'),
mysql = require('mysql'),
app = express(),
connectionpool = mysql.createPool({
host : 'localhost',
user : 'root',
password : 'password',
database : 'database'
});
app.get('/:transcript', function(req,res){
var var1 = req.param('transcript');
exports.var1 = var1;
var queries = require('./queries'),
query1 = queries.query1;
//Connection to MySQL
connectionpool.getConnection(function(err, connection) {
if (err) {res.send({result: 'error connection'})}
connection.query(query1, function(err, rows) {
if (err) {res.send({result: 'error query1'})};
counter = 0; root = {};
rows.forEach(function (row) {
build_actor(row.Transcript_alias, function(exprobject1) {
counter += 1;
//Defining and filling objects
main = {};
main.Official_transcript_name = row.Transcript_name;
main.Expression = exprobject1;
root[row.Transcript_alias] = main;
if (counter == rows.length) {
res.write(JSON.stringify(root, null, '\t'));
res.end();
}
});
});
connection.release();
});
//CallBack
function build_actor(transcript, callback) {
//Other, secondary queries:
var query2 = 'SELECT * FROM expression WHERE transcript_alias = "' + transcript + '";',
connection.query(query2, function(err, rows1) {
if (err) {res.send({result: 'error query2'})}
var exprobject2 = {},
exprobject1 = {};
for (i = 0; i < rows1.length; i++) {
Conditions = rows1[i].conditions;
Tissue = rows1[i].tissue;
FPKM = rows1[i].FPKM;
exprobject2[Tissue] = FPKM;
if (Conditions in exprobject1) {
exprobject1[Conditions].push(exprobject2);
} else {
exprobject1[Conditions] = [];
exprobject1[Conditions].push(exprobject2);
}
}
callback(exprobject1);
});
}
});
});
app.listen(3000);
console.log('Listening on port 3000');
This script calls a required file where there are my queries:
//queries.js
var server = require('./server'),
query1 = 'SELECT distinct(transcript_alias)\
FROM transcript_features \
WHERE f.transcript_alias = "' + var1 + '";';
exports.query1 = query1;
I go to the contents of this script this way:
http://localhost:3000/AC149829.2_FGT004
http://localhost:3000/AC148152.3_FGT007
When I first visit http://localhost:3000/AC149829.2_FGT004, the API shows the correct results for the variable AC149829.2_FGT004. However, when changing the variable to AC148152.3_FGT007, it continues showing the information for the variable AC149829.2_FGT004. In order to see the results for AC148152.3_FGT007, I must kill the script, call it again, and visit for the first time http://localhost:3000/AC148152.3_FGT007. In conclusion, results are not refreshed.
How is that? I tried with a simple:
app.get('/:transcript', function(req,res){
var input = req.param('transcript');
res.send(input);
});
but it works well...
EDIT. I found the source of my problem. query1 is always the same. The script only calls once:
exports.var1 = var1;
var queries = require('./queries'),
query1 = queries.query1;
There's a way to overcome this limitation?
I found the solution for my problem. As
//server.js
exports.var1 = var1;
var queries = require('./queries'),
query1 = queries.query1;
is executed once and remains in the cache, I changed my code without exporting var1:
//server.js
var queries = require('./queries'),
query1 = queries.query1 + var1;
and
//queries.js
var server = require('./server'),
query1 = 'SELECT distinct(transcript_alias)\
FROM transcript_features \
WHERE f.transcript_alias = ';
exports.query1 = query1;
In other words, I import my query to server.js without any variable. The variable is assigned at server.js.

Categories

Resources