Interfacing Node-Postgres, Express JS and Node JS

Home / Uncategorized / Interfacing Node-Postgres, Express JS and Node JS

Question:
Issue:
So, I’m having a typical issue while trying to store my pagecounter in a database. I’m using Node JS and Node PG through a remote console so I don’t know the versions. It’s not a connection based problem or anything wrong with my database either as I’ve tested it using a sample database.

P.S: I’m new to both javascript and databases(especially) and this might be a silly error so bear with me.

As you can see from the code, I’m using an old syntax as I’m following an year old video tutorial as part of my course.

I’ve gone through this question but I don’t think that’s the issue here.

Observations:
Everytime, I restart the server, it starts from server.js’s counter.
Henceforth, the counter gets incremented for every alternate manual refresh.
I’ve observed that even my Database is getting altered accordingly.

Debug:
Re-querieng the same SELECT doesn’t work.
Using a temp variable to hold the count and checking against it has no affect either.
The request status toggles between 200 and 304 for increment and 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 FROM users WHERE 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 as seen in: ‘+JSON.stringify(counter));
else pool.query("UPDATE users SET pageviews = $1 WHERE name = ‘Hyperclaw79’",[cnt]);
return res.send(flag +": " + cnt.toString());
});

I’ve set my pageviews in DB to 850 to get a clarity on what exactly is going on.

However, it has this typical alternating behaviour. Here is the output I’ve recieved:From server.js: 801
From query: 851
From query: 802
From query: 852
From query: 803
From query: 853
.
.
.
.
.

It is clear that, the SELECT query isn’t being sent the first time. Henceforth, all the transactions are being done from the database itself. However, even though the database is supposed to get overwritten by 801 and it should increment accordingly, somehow the old value is returning back alternatively and that too, incremented.

It would be really helpful if anyone can find the bug or indicate if I’m mistaken in my logic as I’ve been at it for a whole day and still am not able to figure this out. Please don’t suggest workarounds using external packages like this as I do not have admin priveldges over the remote machine. Well, thanks in advance if you’re able to figure this out.


Answer:
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 FROM users WHERE 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 as seen in: ‘+JSON.stringify(counter));
else pool.query("UPDATE users SET pageviews = $1 WHERE name = ‘Hyperclaw79’",[cnt]);
return res.send(flag +": " + cnt.toString());

}
});

});

In the original code, the callback for pool.query is called after the counter is read, so the counter is not actually updated when counter is stored in the variable cnt.
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *