0

Good evening,

I am trying to play around with the basics of web development, and have a React frontend, and Node backend.

On the backend, I have a route, that when called, prints a query to the console:

app.post('/messaging', async (req, res) => {

  console.log("WE HIT THE BACKEND")
  var authName = req.body.combo.split(",")[1]
  var smsName = req.body.combo.split(",")[0]

  client = new Client({
       user: 'postgres',
       host: 'localhost',
       database: 'Hermes',
       password: 'a',
       port: 5432,
   });

   console.log(client)

  await client.query("SELECT fname FROM Users WHERE username = '" + 'chakata' + "';", (err, result) => {
    console.log("QUERY WORK?");
    if(err){
      console.log("Query errored out...")
      console.log(err);
    }
    else{
      console.log(result);
      console.log("Successful Query")
      const { username } = req.body;
    }
  });
  console.log("DO WE GET PAST THIS SPOT?")
});

However, something peculiar happens - the route is successfully accessed, the client is successfully connected to postgresql, and the final console print occurs.

It looks like:

WE HIT THE BACKEND
Client {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  connectionParameters: ConnectionParameters {
    user: 'postgres',
    database: 'Hermes',
    port: 5432,
    host: 'localhost',
    binary: false,
    options: undefined,
    ssl: false,
    client_encoding: '',
    replication: undefined,
    isDomainSocket: false,
    application_name: undefined,
    fallback_application_name: undefined,
    statement_timeout: false,
    idle_in_transaction_session_timeout: false,
    query_timeout: false,
    connect_timeout: 0
  },
  user: 'postgres',
  database: 'Hermes',
  port: 5432,
  host: 'localhost',
  replication: undefined,
  _Promise: [Function: Promise],
  _types: TypeOverrides {
    _types: {
      getTypeParser: [Function: getTypeParser],
      setTypeParser: [Function: setTypeParser],
      arrayParser: [Object],
      builtins: [Object]
    },
    text: {},
    binary: {}
  },
  _ending: false,
  _connecting: false,
  _connected: false,
  _connectionError: false,
  _queryable: true,
  connection: Connection {
    _events: [Object: null prototype] { newListener: [Function] },
    _eventsCount: 1,
    _maxListeners: undefined,
    stream: Socket {
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: null,
      _readableState: [ReadableState],
      readable: false,
      _events: [Object: null prototype],
      _eventsCount: 1,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: false,
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: null,
      _server: null,
      [Symbol(asyncId)]: -1,
      [Symbol(kHandle)]: null,
      [Symbol(kSetNoDelay)]: false,
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: null,
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kCapture)]: false,
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0
    },
    _keepAlive: false,
    _keepAliveInitialDelayMillis: 0,
    lastBuffer: false,
    parsedStatements: {},
    ssl: false,
    _ending: false,
    _emitMessage: false,
    [Symbol(kCapture)]: false
  },
  queryQueue: [],
  binary: false,
  processID: null,
  secretKey: null,
  ssl: false,
  _connectionTimeoutMillis: 0,
  [Symbol(kCapture)]: false
}
DO WE GET PAST THIS SPOT?

But something interesting happens - it appears that the client.query NEVER runs. No prints in the query run, and it gives no indication that something happened. What key fundamental am I missing?

Thanks so much for your time!

  • I....uh.... Forgot to include "client.connect()" after the authentication. Thats why its not working. Because I'm an idiot. I'm going to leave the question up for other people to reference unless someone provides a link to a similar question. – Jackson Ennis Dec 08 '20 at 06:35
  • 1
    Make an answer and mark it as correct. That’s better than a comment for future reference. – Faraz Dec 08 '20 at 06:59
  • Apart from this you are mixing `async/await` with callbacks which is why `DO WE GET PAST THIS SPOT?` will be printed before the query is executed. Read https://stackoverflow.com/questions/45338311/node-js-async-await-dealing-with-callbacks for more information. – eol Dec 08 '20 at 10:27

0 Answers0