Saturday, 31 March 2018

Help with database query - can I un-async it?

I'm about to defenestrate this computer, so forgive the wall of text please.I've had this problem before twice. The first time, it was because the returned object was actually an unfulfilled promise, and I was able to get around it by throwing all my code inside of the block so that it would execute in there. Not exactly an ideal solution but it worked and I was in a hurry.The second time I was using pg-promise instead of just pg, and when this issue popped up I thought "it must be that damn promise again", so instead of tearing my hair out I just uninstalled pg-promise and went with the regular postgres package for nodejs instead. Well, the problem is still here and I'm tearing my hair out.I'm making an API. I have a controller with functions such as getPayment and postPayment and so on. At some point in these functions, I need to get information from or into the database. In another file, which I called postgres.js, I have a connection to the database and then functions which I can call to insert data, select, etc. Here is postgres.js as it is right now (this works but only for printing to console):module.exports = { select: select }; const { Client } = require('pg'); const client = new Client({ user: 'myUser', host: 'localhost', database: 'myDB', password: 'myPassword', port: 5432, }); client.connect(); function select(){ client.query('SELECT * FROM payments', (err, res) => { console.log(err ? err.stack : res.rows[0].id); client.end(); }); } As you can see right now all it does is get the payments from the table and print a value (in this case, the first row's id) to console. Here is the code that calls this function, from the controller (payments.js):var pg = require('../../config/postgres.js') ... function getPaymentInfo(params){ // get info from the DB // disregard the fact that I'm not sending any params right now, this is for testing var result = pg.select(); console.log(result); console.log(typeof result); } Calling pg.select() does in fact activate the select function on postgres.js (and the console log correctly prints the data in res.rows[0].id) however when I try putting a return res.rows[0].id there in order to actually return the information that was retrieved from the database, what comes back into the var result is "undefined" according to the console. Here are a few examples of things I've tried to do to get the data out:function select(){ var i = ''; client.query('SELECT * FROM payments', (err, res) => { //console.log(err ? err.stack : res.rows[0].id); i = err? err.stack : res.rows[0].id; client.end(); }); return i; } I tried this with and without commenting the console.log line, either way it doesn't work.function select(){ client.query('SELECT * FROM payments', (err, res) => { //console.log(err ? err.stack : res.rows[0].id); return err ? err.stack : res.rows[0].id; //client.end(); }); } Instead of printing t console, just return the result of that little error check up there... returns undefined.function select(){ return client.query('SELECT * FROM payments', (err, res) => { //console.log(err ? err.stack : res.rows[0].id); // Hello World! client.end(); return err ? err.stack : res.rows[0].id; }); } Returning the return from client.query(etc etc);.. same thing as before. Also tried without the explicit return, without checking for error, all the same.Here's the last test I tried:function select(){ var i = 'original i'; client.query('SELECT * FROM payments', (err, res) => { console.log(err ? err.stack : res.rows[0].id); // Hello World! if (err){ i = "i has changed!"; } else { i = "i has not changed!"; } client.end(); }); return i; } The console shows this:original i string 2 That last "2" being res.rows[0].id.I really believe this must have something to do with the blasted "promises" and asyncronous stuff. But my previous "solution" won't fit here. I can't just do everything inside of that block where I have access to res. I have to be able to return some data back to getPaymentInfo().It seems that client.query(...) is waiting for every line in getPaymentInfo() to be ran before actually doing anything. I can see that because in the console, the results of console.log(result) and console.log(typeof result) appear before the line that shows information from the table (from console.log(err ? err.stack : res.rows[0].id) ). That is absolutely infuriating.I saw some examples out there that used await, but when I tried to add that copying the examples, it gave me a syntax error as if it didn't recognize what await is supposed to be. There was an example with yield as well, same error. It doesn't know what those commands mean. I'm using the latest version of nodejs (v8.10.0).I also tried using then to get it to wait:function generatePaymentInfo(params){ ... pg.select().then(function(v) { console.log(v); }); ... } It gives me this error:TypeError: Cannot read property 'then' of undefined What's going on here? How do I get it to do things in the right order? All I want is to get some information from a table. Every example for the pg package (and pg-promise as well) I see out there just has a console.log() representing "success". In reality what anyone would want is to have actual access to the data. The level of shenanigans going on here is officially too much. This isn't AJAX. I don't need this to be asynchronous! It's all happening in the server! It could very well go in there, get the data, return it, and keep going just how god intended it to! But how??

Submitted March 31, 2018 at 10:59PM by geleiademocoto

No comments:

Post a Comment