sql - Interfacing Node-Postgres, Express JS and Node JS -
issue:
so, i'm having typical issue while trying store pagecounter in database. i'm using node js , node pg through remote console don't know versions. it's not connection based problem or wrong database either i've tested using sample database.
p.s: i'm new both javascript , databases(especially) , might silly error bear me.
as can see code, i'm using old syntax i'm following year old video tutorial part of course.
i've gone through question don't think that's issue here.
observations:
- everytime, restart server, starts server.js's counter.
- henceforth, counter gets incremented every alternate manual refresh.
- i've observed database getting altered accordingly.
debug:
- re-querieng same select doesn't work.
- using temp variable hold count , checking against has no affect either.
- the request status toggles between
200
,304
increment , no change respectively.
code:
var express = require('express'); var morgan = require('morgan'); var path = require('path'); var app = express(); app.use(morgan('combined')); var crypto = require('crypto'); var pool = require('pg').pool; var config = { user: 'dragonlordthota717', database: 'dragonlordthota717', host: 'db.imad.hasura-app.io', port: '5432', password: process.env.db_password }; var pool = new pool(config); var counter = {"pageviews":800}; var flag = "from server.js"; app.get('/counter', function (req, res){ pool.query("select pageviews users name='hyperclaw79'",function(err,result){ if(err){ res.status(500).send("flag: "+err.tostring()); } else{ counter = result.rows[0]; flag = "from query"; } }); var cnt = counter.pageviews; cnt = parseint(cnt)+1; if(isnan(cnt)) res.status(500).send(flag+': returned nan seen in: '+json.stringify(counter)); else pool.query("update users set pageviews = $1 name = 'hyperclaw79'",[cnt]); return res.send(flag +": " + cnt.tostring()); });
i've set pageviews
in db 850 clarity on going on.
however, has typical alternating behaviour. here output i've recieved:
from server.js: 801 query: 851 query: 802 query: 852 query: 803 query: 853 . . . . .
it clear that, select query isn't being sent first time. henceforth, transactions being done database itself. however, though database supposed overwritten 801 , should increment accordingly, somehow old value returning alternatively , too, incremented.
it helpful if can find bug or indicate if i'm mistaken in logic i've been @ whole day , still not able figure out. please don't suggest workarounds using external packages this not have admin priveldges on remote machine. well, in advance if you're able figure out.
the following should work
var express = require('express'); var morgan = require('morgan'); var path = require('path'); var app = express(); app.use(morgan('combined')); var crypto = require('crypto'); var pool = require('pg').pool; var config = { user: 'dragonlordthota717', database: 'dragonlordthota717', host: 'db.imad.hasura-app.io', port: '5432', password: process.env.db_password }; var pool = new pool(config); var counter = {"pageviews":800}; var flag = "from server.js"; app.get('/counter', function (req, res){ pool.query("select pageviews users name='hyperclaw79'",function(err,result){ if(err){ res.status(500).send("flag: "+err.tostring()); } else{ counter = result.rows[0]; flag = "from query"; var cnt = counter.pageviews; cnt = parseint(cnt)+1; if(isnan(cnt)) res.status(500).send(flag+': returned nan seen in: '+json.stringify(counter)); else pool.query("update users set pageviews = $1 name = 'hyperclaw79'",[cnt]); return res.send(flag +": " + cnt.tostring()); } }); });
in original code, callback pool.query
called after counter read, counter not updated when counter stored in variable cnt
.
Comments
Post a Comment