0

I'm using express, pg, javascript to pull data from my PostgreSQL db. after running the below code. I'm not getting the response from the result

function onConnect(err, client, done) {
  if (err){
    console.error (err);
    process.exit(1);
  }

  client.query(findDataInDatabase('Username', 'Email'), function(err, result){
    if(err){
      return console.error('error happened', err)
    }

    var someString = result.rows[0].Email;
    done()
    console.log("onConnect is returning this: " + someString) 
    return someString;
    })
}

app.get('/', function (req, res) {
  console.log('Received a request');

  pg.connect(conString, onConnect, function(err, result){
  if(err){
    return console.error('pg.connect error : ', err);
  }

  res.json(result);
}

); })

Eric
  • 954
  • 2
  • 14
  • 23
  • Why are you calling `done()` at the _beginning_ of the callback? Call it at the end, after data treatment – Jeremy Thille May 25 '17 at 18:10
  • @FelixKling it has nothing to do with the duplicate you marked. Clearly OP uses callbacks correctly. I think the problem is the placement of the `done()` function. – Jeremy Thille May 25 '17 at 18:12
  • @JeremyThille: The fact that the OP tries to do `return someString;` tells me that they do not understand the asynchronous nature of this API. – Felix Kling May 25 '17 at 18:13
  • no im very new to callbacks, i understand the concept of it and i think of asynchronous design like multithreading where you can have things running simultaneously. But I don't think i fully grasp when it happens. – Eric May 25 '17 at 18:17
  • But they're trying to return it in the callback, don't they? Not outside. – Jeremy Thille May 25 '17 at 18:17
  • @JeremyThille: Sure, and that's still wrong. The OP wants to process the result of an asynchronous operation. The duplicate explains how they can do that. – Felix Kling May 25 '17 at 18:21
  • @OP you have to use `res.send( someString )` instead of `return someString`. So you need to pass `res` to the `onConnect` function. – Jeremy Thille May 25 '17 at 18:21
  • i checked the console, the console log calls shows that the result is undefined before the Connect function is finished passing the variable. @JeremyThille how does using res.send fix the issue in this case? I thought that's for rendering on browser? – Eric May 25 '17 at 18:37
  • Precisely. Send your data back to the browser. – Jeremy Thille May 25 '17 at 18:39
  • I would write a proper answer with the code and all, but @Felix locked the question, I can only post comments. – Jeremy Thille May 25 '17 at 18:40
  • Well i've attempted to add a callback function to the function, but it's still not giving me any results. I updated the question with my current code. Am I writing the call back wrong? – Eric May 25 '17 at 21:02
  • @FelixKling It's been 4 hours and im still stuck on this. I've read and watch videos on Promises, Callbacks and i still cant get this working. Any assistance on this would be really helpful. – Eric May 25 '17 at 22:24
  • Maybe this helps: https://jsfiddle.net/k7ocdeok/1/ – Felix Kling May 25 '17 at 23:05
  • 1
    This works! Omg I spent so long trying to figure this out. Theres so many examples with jQuery but none with Postgre. And it confused me greatly, Thank you so much for clearing this up. now i'm gonna study this and convert it to memory. – Eric May 26 '17 at 00:52

0 Answers0