Having some trouble parsing the results returned from a node "mysql" query.
Node version 7.8.0
Running CentOS AWS environment.
I developed the node app in Windows Visual Studio CE 2017, and it was working fine, but when porting it to Linux, it's not actually allowing me to access the user_id variable within the results.
I have included lots of variations in trying to access the variable, however the correct one I believe should be:
rows[i].user_id
All the others are just there as a reference to show that they don't work.
Code:
'use strict';
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var mysql = require("mysql");
var con = mysql.createConnection(dbConfig);
con.connect(function (err) {
if (err) {
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
var q = "select * from table";
con.query(q, function (err, rows) {
for (var i = 0; i < rows.length; i++) {
var row = JSON.stringify(rows[i]);
var rowParsed = JSON.parse(row);
console.log(row);
console.log(" row[0].user_id " + row[0].user_id );
console.log(" row.user_id " + row.user_id );
console.log(" parsed " + JSON.parse(row));
console.log(" rowParsed.user_id " + rowParsed.user_id);
console.log(" rowParsed['user_id'] " + rowParsed['user_id']);
console.log(" rows[i].user_id " + rows[i].user_id)
console.log(" rows[i]['user_id'] " + rows[i]['user_id']);
console.log(rows[i]);
}
});
Results:
Connection established
[{"user_id":2}]
parsed [object Object]
row[0].user_id undefined
row.user_id undefined
rowParsed.user_id undefined
rowParsed['user_id'] undefined
rows[i].user_id undefined
rows[i]['user_id'] undefined
[ RowDataPacket { user_id: 2 } ]
POST /users/register 200 120.026 ms - 22
Related
I am creating a nodejs module which retrieve some data from a mysql database and insert into another mysql database after some data processing. My main requirement is to make the module alive 24 hours even there is no data in the first database.. it will just keep checking for any new data. But unfortunately the module just doing nothing after few minutes of running. My function is as follows:
var to_pool = mysql.createPool({
connectionLimit: 100,
host: 'localhost',
user: 'username',
password: 'password',
database: 'toDatabase',
multipleStatements: true
});
var from_pool = mysql.createPool({
connectionLimit: 100,
host: 'localhost',
user: 'username',
password: 'password',
database: 'fromDatabase'
});
get_data(to_pool, from_pool);
var items_per_query = 100;
function get_data(to_pool, from_pool) {
from_pool.getConnection(function (err, from_connection) {
if (err) throw err; // not connected!
//main database query
from_connection.query("SELECT p.*, d.uniqueid as imei FROM tc_positions p left join tc_devices d on d.id = p.deviceid order by p.id desc limit " + items_per_query, function (err, result, fields) {
if (err) throw err;
var items = [];
if (Object.keys(result).length > 0) {
Object.keys(result).forEach(function (key) {
var x = result[key];
items.push({ 'id': x['id'], 'table_name': x['imei'], 'table_columns': table_columns_list });
});
}
if (items.length >= items_per_query) {
var items_to_be_removed = [];
let imei_insert = "";
let insert_data = "";
for (var x = 0; x < items.length; x++) {
let all_values = "";
let i = 0;
for (let v of Object.values(items[x]['table_columns'])) {
i++;
all_values += "'" + v + "'";
if (i < Object.keys(items[x]['table_columns']).length) {
all_values += ",";
}
}
insert_data += "INSERT INTO " + items[x]['table_name'] + "(dt_server,dt_tracker,lat,lng,altitude,angle,speed,params,fix_time,accuracy,network) VALUES(" + all_values + "); ";
items_to_be_removed.push(items[x]['id']);
if (items_to_be_removed.length == items_per_query) {
var final_query = imei_insert + ' ' + createTable + ' ' + insert_data;
to_pool.getConnection(function (err, platform_connection) {
if (err) throw err;
platform_connection.query(final_query, function (err, results, fields) {
if (err) throw err;
var ids = items_to_be_removed.join(",");
from_connection.query("DELETE FROM tc_positions where id IN(" + ids + ")", function (err, results, fields) {
if (err) throw err;
console.log('removed ' + items_to_be_removed.length + ' rows from traccar');
items_to_be_removed = [];
insert_data = "";
from_connection.destroy();
platform_connection.destroy();
// after finish all task call the same function again
return get_data(to_pool, from_pool);
});
});
});
}
}
}
else {
setInterval(function () { get_data(to_pool, from_pool); }, 10000);
}
});
});
}
the get_data() function is being called every 10 secs but the "main database query" portion never execute after sometimes. Is there any way to execute the database query again and again as the get_data() function call?
it is better to use a package manager like PM2 and start your script like this
pm2 start app.js
no need to setup intervals in your code, let the code run and exit, PM2 will restart it automatically when it stops running, this will make your code alive 24 hours as per your requirement, you can also setup delays or setup restart strategies
In this code, I am sending a POST request to the server which contains email-id and using that email-id I am retrieving Information from the database.
I am testing this from postman "http://127.0.0.1:80/echo" POST request after sending a request first time it shows in response
{
"source": "webhook-echo-sample"
}
and the second time it gives me proper result
{
"speech": "Name:xyz Mobile.no:123456789Address:qweCollege:azx Email:cvb#gmail.com",
"source": "webhook-echo-sample"
}
Why my first request to API is going fail?
"use strict";
const express = require("express");
const restService = express();
const bodyParser = require("body-parser");
const os = require('os');
var mysql = require("mysql");
var final;
var Name = "Name:";
var Mobileno = "Mobile.no:";
var College = "College:";
var Address = "Address:";
var Email = "Email:";
restService.use(
bodyParser.urlencoded({
extended: true
})
);
restService.use(bodyParser.json());
restService.post("/echo", function(req, res) {
var con = mysql.createConnection({
host: "abc.com",
user: "xyz123",
password: "123456",
database: "seller1"
});
var email = req.body.result.parameters.echoText
var sql = 'SELECT * FROM seller1 WHERE email_id=' + mysql.escape(email);
con.query(sql, function(error, results) {
if (!error) {
for (var i = 0; i <= results.length - 1; i++) {
final = Name + results[i].Name + "\n" + Mobileno + results[i].mobile_no + "\n" + Address + results[i].Address + "\n" + College + results[i].College + "\n" + Email + results[i].email_id;
}
} else {
return [];
}
});
return res.json({
"speech": final,
"source": "webhook-echo-sample"
});
});
restService.listen(process.env.PORT || 80, function() {
console.log("Server up and listening");
});
I think it's because of javascript Asynchronous nature
try sending the response inside !error check as shown below
restService.post("/echo", function(req, res) {
var con = mysql.createConnection({
host: "abc.com",
user: "xyz123",
password: "123456",
database: "seller1"
});
var email = req.body.result.parameters.echoText
var sql = 'SELECT * FROM seller1 WHERE email_id=' + mysql.escape(email);
con.query(sql, function(error, results) {
if (!error) {
for (var i = 0; i <= results.length - 1; i++) {
final = Name + results[i].Name + "\n" + Mobileno + results[i].mobile_no + "\n" + Address + results[i].Address + "\n" + College + results[i].College + "\n" + Email + results[i].email_id;
}
return res.json({
"speech": final,
"source": "webhook-echo-sample"
});
} else {
return [];
}
});
});
I have this really simple get request that returns json that I am trying to implement. I have followed the tutorials for Express Web Framework REST API, but for some reason I keep getting the same error
ERROR:
TypeError: res.status is not a function
or
TypeError: res.json is not a function
index.js:
var express = require('express');
var router = express.Router();
var pg = require('pg');
var connectionString = 'pg://postgres:postgres#postgres/feed';
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/api/leaderboard', function(resp, req){
var results = [];
pg.connect(connectionString, function(err, client, done){
if(err){
done();
console.log(err);
return res.status(500).json({ success: false, data: err});
}
var query = client.query("SELECT * FROM log WHERE (logged >= date_trunc('week', CURRENT_TIMESTAMP - interval '1 week') AND logged <= date_trunc('week', CURRENT_TIMESTAMP));");
var counter = 0;
var b1 = {};
var b2 = {};
var b3 = {};
var b4 = {};
b1.energy_sum_week = 0;
b2.energy_sum_week = 0;
b3.energy_sum_week = 0;
b4.energy_sum_week = 0;
b1.zne_sum_week = 30000;
b2.zne_sum_week = 30000;
b3.zne_sum_week = 30000;
b4.zne_sum_week = 30000;
query.on('row', function(row){
//results.push(row);
if(row['address'] == 215){
b1.energy_sum_week = row['kitchen'] + row['plugload'] + row['lights'] + row['ev'] + row['hvac'] + row['instahot'] - row['solar'];
}
else if (row['address'] == 1590) {
b2.energy_sum_week = row['kitchen'] + row['plugload'] + row['lights'] + row['ev'] + row['hvac'] + row['instahot'] - row['solar'];
} else if (row['address'] == 1605) {
console.log(row);
b3.energy_sum_week = row['kitchen'] + row['plugload'] + row['lights'] + row['ev'] + row['hvac'] + row['instahot'] - row['solar'];
} else if (row['address'] == 1715) {
b4.energy_sum_week = row['kitchen'] + row['plugload'] + row['lights'] + row['ev'] + row['hvac'] + row['instahot'] - row['solar'];
}
});
query.on('end', function(){
done();
//make zne lower than everything
results.push(b1);
results.push(b2);
results.push(b3);
results.push(b4);
resp.json(results);
});
});
});
module.exports = router;
It seems like it can't recognize the response object. Tried a bunch of different things like passing in the request and response's to the query callbacks, and using promises.
Getting kinda desperate here :/
The res variable doesn't exist in the current context, you probably expect that the line
router.get('/api/leaderboard', function(resp, req){
had this form
router.get('/api/leaderboard', function(req, res){
You are passing resp as the req object and the req as the resp object.
Try changing the order.
router.get('/api/leaderboard', function(req, resp){...}
I made some code to parse things from some file.log it got 2 different lines that one is line of command and finishing line. So there are 1 000 000 lines of log which is 500 000 entries into DB.
Problem inserting and updating database is to slow.
function getDate(){
var date = new Date().getTime() + (2 * 60 * 60 * 1000);
return new Date(date).toISOString().replace(/T/, ' ').replace(/\..+/, ''); // DATETIME in format yyyy-mm-dd hh:mm:ss
}
var detailLog = false;
var express = require("express");
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'logger'
});
var app = express();
connection.connect(function(err) {
if (!err) {
console.log(getDate(), "Database is connected ... \n");
} else {
console.log(getDate(), "Error connecting database ... \n");
}
});
var file = 'file.log';
var fs = require('fs');
var rl = require('readline').createInterface({
input : require('fs').createReadStream(file)
});
rl.on('line', function(line) {
//searching 'command' since it is in every odd line
if (line.search('command') >= 0) {
//parsing every odd line of log
// insert in logs table
var queryINS = "";
if (detailLog)
console.log(getDate(),'shooting INSERT ');
connection.query(queryINS,
function(err, rows, fields) {
if (err) {
console.log(getDate(), 'Error while performing
insert into logs query: ', queryINS,err);
}
});
if (detailLog)
console.log(getDate(),'INSERT done');
//searching 'Request finished' since it is in every even line
} else if (line.search('Request finished') >= 0) {
//parsing every even line of log
// update logs table
var queryUPD = "";
if (detailLog)
console.log(getDate(),'shooting UPDATE ');
connection.query(queryUPD,
function(err, rows, fields) {
if (err) {
console.log(getDate(), 'Error while performing update
logs query: ',queryUPD,err);
}
});
if (detailLog)
console.log(getDate(),'UPDATE done');
}
}).on('close', function() {
console.log(getDate(), 'Inserted logs into table.');
});
I have just started coding with node.js, I understand that node.js is asynchronous but not sure how to deal with this problem.
I'm querying postgresql and building a JSON as follows,
I added my code :
var express = require('express');
var router = express.Router();
var pg = require('pg');
var client = require('../routes/database.js');
var cookieParser = require('cookie-parser');
var dateFormat = require('date-format');
var async = require('async');
var today = dateFormat(new Date());
router.post('/conversation/my', function(req, res) {
var userId = 59;
var limit = 10;
var offset = 0;
var postgresql = "select id from conversation where party_id = '" + userId + "' and reply_id = 0 order by created_on desc limit " + limit + " offset " + offset + "";
var postsJSON = { };
var arr = new Array();
client.query(postgresql, function(err, data) {
if (err) {
console.log(err);
return rollback(client);
}
var rows = data.rows;
for ( i = 0; i < rows.length; i++) {
var post = rows[i];
var post_obj = {};
post_obj.id = post.id;
getConversationResponse(post.id, function(err, res) {
if (!err) {
post_obj.actor = res;
arr.push(post_obj);
console.log(JSON.stringify(arr));
res.send({
data : arr
});
}
});
}
});
});
function getConversationResponse(conversation_id, cb) {
client.query('SELECT * FROM people WHERE id ='+conversation_id+';', function(err, actor) {
client.query('SELECT * FROM users WHERE id ='+conversation_id+';', function(err, user) {
var actor_obj = {};
actor_obj.id = user.id;
actor_obj.name = user.name;
actor_obj.email = user.email;
client.end();
cb (null,actor_obj);
});
});
}
module.exports = router;
But i am getting error :
{ [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', syscall: 'write' }
events.js:72
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at errnoException (net.js:901:11)
at Object.afterWrite (net.js:718:19)
npm ERR! weird error 8
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
Your client.end(); should not be in the function. It should be after the first client.query for conversations has completed. Currently what happens is that for each conversation, you call the function to get the people/users, where you end the client. So the next conversation row cannot get the people/users.
Also, ideally you should join the three tables and get all the data in one shot.
var postgresql = "select id, people.*, users.* from conversation, people, users where party_id = '" + userId + "' and people.id=conversation.id and users.id=conversations.id and reply_id = 0 order by created_on desc limit " + limit + " offset " + offset + "";