I am connecting my application in node.js to postgresql and getting the results and making it into Json. But when I am making a request to the node.js route I am getting the results appended into the variable in which i am storing the db result instead of getting loaded everytime.
The code snippets is like:
function getDBData(request,response,next){
console.log(request.params);
var client = new pg.Client(connStr);
// Connecting to the client
client.connect(function(err) {
if (err) {
return console.error('could not connect to postgresq',err);
}
console.log('CONNECTION ESTABLISHED');
})
// Querying the DB
var query = client.query('SELECT * FROM city');
var i = 0;
query.on('row', function(row) {
if(i == 0){
rowData = rowData + row;
i++;
}else{
rowData = rowData +","+ row;
i++;
}
});
swiftarr= JSON.stringify(rowData);
//client.end();
response.send(200,swiftarr).end();
}
app.get('/mycity',getDBData)
in the swiftarr variable the results are getting appended instead of getting replaced with every request.
The results i am getting on the browser is like
"[object Object],[object Object][object Object],[object Object]"
Instead with every request I should get something like this
[object Object],[object Object]
Since in my db i have only two records.
For ex.
if i make the first request two records are coming into the result variable
next time instead of getting replaced it's getting appended and getting four records into the result variables.
I don't know whether i am making any logical error.