I am using a query to a postgres db to store the result in a variable in a GET request on the server-side of an Express app.
My query returns the issueID that i am looking for when i run it in psql, but I am having a hard time getting the response to store in a variable so I can send the response to the client side.
My code looks like this:
const pg = require('pg');
const connection = 'postgres://aa2:aa2@localhost:5432/bugtrackingdb';
const client = new pg.Client(connection) ;
client.connect();
app.get('/tracking', function(req, res) {
var sql = "SELECT bugtracking.issues.issueID FROM bugtracking.issues WHERE bugtracking.issues.issueID = (SELECT MAX(bugtracking.issues.issueID) FROM bugtracking.issues);"
var issueID;
client.query(sql, function(err, result) {
if (err){
console.log("error in tracking");
}
issueID = JSON.stringify(result.rows[0]);
console.log("Tracking Result" + issueID);
//GET results
res.json(res.statusCode);
// res.json(issueID);
console.log("Selected Bug Tracking IssueID. Status Code: " + res.statusCode);
});
});
How do I get the data from the query to store the data in the issueID variable to send it to the client-side of my app?
I am using body-parser also.
EDIT: It's almost as if the query isn't even executing because I am not able to get my console.log statements to print. This is my front-end code to help understand better:
$(window).load(function() {
$.ajax({
type: 'GET',
url: 'http://139.169.63.170:' + port + '/tracking',
dataType: 'json',
cache: true,
success: function (data) {
console.log("DEBUG DONE WITH CAPTURING tacking#: " + data);
if (data === Number(200)) {
issueID = issueID + 1;
$(".trackingID").val(issueID);
}
}
});
console.log("Window loads & Ajax runs???");
});
2nd EDIT:
Here are the results of running my query in psql. I am trying to get the number '33' to return from my get request:
bugtrackingdb=# SELECT bugtracking.issues.issueID FROM bugtracking.issues WHERE bugtracking.issues.issueID = (SELECT MAX(bugtracking.issues.issueID) FROM bugtracking.issues);
issueid
---------
33
(1 row)
AFter a few changes this is what I am getting to print from the back end console.log statements:
Tracking Result: [object Object]
Selected Bug Tracking IssueID. Status Code: 304
EDIT (Getting closer):
I stringified the result.rows[0] from the object returned from my query. I am now able to get the result to print to the server-side console.log which gives me the following results:
Tracking Result: {"issueid":"37"}
Selected Bug Tracking IssueID. Status Code: 304
status: 200
However, when I uncomment the line res.json(issueID)
I get an error.
How do I send my JSON object back to the client side so that I can display it as needed?
app.get('/tracking', function(req, res) {
var sql = "SELECT bugtracking.issues.issueID FROM bugtracking.issues WHERE bugtracking.issues.issueID = (SELECT MAX(bugtracking.issues.issueID) FROM bugtracking.issues);"
var issueID;
client.query(sql, function(err, result) {
if (err){
console.log("error in tracking");
}
issueID = JSON.stringify(result.rows[0]);
console.log("Tracking Result" + issueID);
// TRY THIS
res.send(issueID);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.