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 + "";
Related
var express = require('express');
var app=express();
var length;
var affiliate = require('flipkart-affiliate');
var url = require('url');
var moment=require('moment');
var mysql = require('mysql');
var body;
var getUrl;
var product;
var offer;
var offer1;
var offer2;
var offer3;
var test1;
var test2;
var test3;
var title=[];
var description=[];
var startTime=[];
var endTime=[];
var json={};
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'coupontest'
});
var client = affiliate.createClient({
FkAffId: 'anandhkum',
FkAffToken: 'eb030998c556443087d3b1a27ac569d0',
responseType: 'json'
});
client.getCategoryFeed({
trackingId: 'anandhkum'
}, function(err, result,getUrl){
if(!err){
body=JSON.parse(result);
getUrl=body.apiGroups.affiliate.apiListings.food_nutrition.availableVariants["v1.1.0"].get;
client.getProductsFeed({
url: getUrl
}, function(err, result){
if(!err){
}else {
console.log(err);
}
});
}
});
connection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
app.get('/',function (req,res) {
client.getAllOffers(null,function(err, resp) {
if (!err) {
offer = JSON.parse(resp);
test1 = offer.allOffersList.length;
res.send(offer);
for(var i=0;i<test1;i++){
description[i]=offer.allOffersList[i].description;
startTime[i]=offer.allOffersList[i].startTime;
endTime[i]=offer.allOffersList[i].endTime;
}
var stmt = "INSERT INTO offers(description,start_time,end_time) VALUES (?, ?, ?)";
connection.query(stmt, [description,startTime,endTime], function (err, result) {
if (err) throw err.message;
console.log("Number of records inserted: " + result.affectedRows);
});
}
else {
console.log(err);
}
});
});
app.listen(3000);
console.log("Listening to port 3000");
the code is about getting the offer details as json data from flipkart.com and store it into the mysql table.
But I'm getting the error
throw err; // Rethrow non-MySQL errors
ER_WRONG_VALUE_COUNT_ON_ROW: Column count doesn't match value count at row 1
I tried using many sql syntax but it's showing the above error
Can anyone help me on solving the above error?
Try doing it this way, as I can see you want to insert multiple result at once so need to pass an array values which I have made in following query also the error "ER_WRONG_VALUE_COUNT_ON_ROW" is due to incorrect no of ? that you were passing in the query as you need to insert multiple no of rows so you need to make sure you get correct ? count.
var queryString = "";
var insertString = "(?,?,?),";
var values = [];
for(var i=0;i<test1;i++){
description[i]=offer.allOffersList[i].description;
startTime[i]=offer.allOffersList[i].startTime;
endTime[i]=offer.allOffersList[i].endTime;
values.push(description[i],startTime[i],endTime[i]);
queryString = queryString + insertString;
}
queryString = queryString.substring(0, queryString.length - 1); // is used to remove last ',' which will get inserted while we are creating queryString
var stmt = "INSERT INTO offers(description,start_time,end_time) VALUES " + queryString ;
connection.query(stmt,values, function (err, result) {
if (err) throw err.message;
console.log("Number of records inserted: " + result.affectedRows);
});
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
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){...}
Community,
I am new at nodejs and now i have a problem i cant solve: The async in javascript/nodejs. How can i handle the following so i can push the usernames to the array?
I already tried to help myself with many different functions but nothing works for me... :/
Sincerely Adhskid.
function getCurrentBetInformations () {
connection.query('SELECT * FROM `BETS` WHERE BET_ACTIVE = "1" LIMIT 1', function(err, rowss, fields) {
if (err) logger.warn('MySQL Error: ' + err.stack);
betid = rowss[0].BET_ID;
betends = rowss[0].BET_END;
connection.query('SELECT * FROM `BETS_BID` WHERE BID_BET_ID=\'' + betid + '\'', function(err, betbids, fields) {
if (err) logger.warn('MySQL Error: ' + err.stack);
var betQuants = new Array();
var betIds = new Array();
var betUsernames = new Array();
var betDates = new Array();
var rowsAffected = betbids.length;
for(i=0; i < rowsAffected; i++) {
betQuants.push(betbids[i].BID_KEYS_COUNT);
betIds.push(betbids[i].BID_ID);
var betSender = betbids[i].BID_SENDER;
connection.query('SELECT `USER_NAME` FROM `USER` WHERE `USER_STEAMID` = \'' + betSender + '\' LIMIT 1', function(err, rows, fields) {
if (err) logger.warn('MySQL Error: ' + err.stack);
console.log(rows[0].USER_NAME);
addUsername(rows[0].USER_NAME);
});
function addUsername (currentUsername) {
betUsernames.push(currentUsername);
}
betDates.push(betbids[i].BID_TIME);
if(betUsernames.length === i) {
execSiteRef();
}
}
function execSiteRef() {
console.log(betUsernames);
sendUserSiteRefresh([betQuants, betIds, betUsernames, betDates], betends);
}
});
});
}
I think your problem comes from this part:
if(betUsernames.length === i) {
execSiteRef();
}
You should iinstead check if the betUsernames array is of the final size:
if(betUsernames.length === rowsAffected) {
execSiteRef();
}
maybe there is more errors though, I did not check closely.
var sender_username = req.session.user_id;
var recipient_username = req.body.recipient_username;
var content = req.body.content;
var sql = ' SELECT sender_username, recipient_username, COUNT(recipient_username) as count FROM message WHERE sender_username = "'+sender_username+'" AND recipient_username = "'+recipient_username+'" GROUP BY sender_username LIMIT 1 ';
var message_no = 0;
var data;
connection.query(sql, function(err, result) {
if (err) {
res.send(err);
}
else {
data = result;
// res.send(data); < - this works
// res.send(result); <- this works
// res.send(result.count); <- undefined
}
});
res.send(data); // undefined (can't seem to save to variable after connection.query())
The res.send(result); seems to work. It gives:
[{"sender_username":"sender","recipient_username":"recipient","count":2}]
I am just trying to get the value for count and save that to a variable, but things like result.count are returning undefined for some reason.
It's because the JSON is an array, so you should access it like the following
res.send(result[0].count);