0

This is my code:

  const queryFirstNames = function (qString) {
    let firstNameMatches;
    client.query('SELECT * FROM famous_people WHERE first_name = $1', [qString], (err, result) => {
      if (err) {
        return console.error('error running query', err);
      }

      firstNameMatches = result.rows;
      return firstNameMatches;
      client.end();
   });
 };

 console.log(queryFirstNames(qString));
});

This code return undefined and doesn't end the connection with the database

But if I console.log firstNameMatches inside the function instead of returning and then just invoke the function without console logging, I get the result I want, and the database connection closes properly.

What I want to do is return the result of this query and use it in another function, so I don't want it to console log the result, but when I try to return the result, it gives me undefined, and doesn't close the database connection either.

2 Answers2

3

I believe that the issue you are having is that when you are returning firstNameMatches, you are returning it just to the callback for client.query. firstNameMatches is set inside of the context of the queryFirstNames function, however you never return that back. That being said, you are also utilizing an async call to client.query. This means that when you return from the queryFirstNames function, chances are you haven't finished querying. Per this, I am pretty sure you want to utilize promises to manage this or the events like one of the other answers.

In addition to this, you are going to want to move your return to before the client.end. To play with my answer, I created a jsfiddle that you can see here. I had to create my own promise via Promise just to mock out the client executing the query.

  const queryFirstNames = function (qString) {
    let firstNameMatches;
    client.query('SELECT * FROM famous_people WHERE first_name = $1', [qString])
    .then((result) => {

      firstNameMatches = result.rows;
      client.end();
      return firstNameMatches;
    })
    .then(f => {
        console.log(f);
    })
    .catch(e => {
      if (err) {
        return console.error('error running query', err);
      }
    });
  };
Chris E
  • 96
  • 1
  • 5
  • Thanks so much for this - what you're saying makes a lot of sense. However, it seems like the firstNameMatches variable only has a value **before** the client.end() - so when I run your code, it returns and console.logs undefined, but if I console.log firstNameMatches from inside the query, it outputs a value. It's as if firstNameMatches doesn't exist outside the scope of the query, even though I defined it in the parent function – Niloo Ravaei Feb 25 '17 at 18:38
  • So I played around with the code a bit more and seems like when I reset the value of firstNameMatches **inside** the query, it doesn't persist, so when I return firstNameMatches outside the query, it will return whatever it's value was originally, not what I set it to inside the query. – Niloo Ravaei Feb 25 '17 at 18:46
0

You are returning the call before you even execute client.end(); This way your client connection will never end. You can do something like this:

const query = client.query(
    'SELECT * FROM famous_people WHERE first_name = $1',
    [qString], 
    (err, result) => {
        if (err) {
            return console.error('error running query', err);
        }
    });

    query.on('row', (row) => {
      results.push(row);
    });

   // After all data is returned, close connection and return results
    query.on('end', () => {
        return res.json(results);
    });
cp1
  • 75
  • 1
  • 5
  • Thanks for sharing this! This helped me close the database connection properly, but it's still returning undefined when I return the value of results, but prints the correct array when i console.log it – Niloo Ravaei Feb 25 '17 at 18:14