0

I am working in a Web Application that connects to my PosgreSQL database, but when I go to the main page it suppose to retrieve the first element of the table actor, but is not retrieving anything, below my code. I checked the connection URL and is the correct username, password, port and database.

const express = require('express');
const bodyParser = require('body-parser');
const session = require("express-session");
const { Client } = require('pg');
const connectionString = 'postgres://postgres:12345@localhost:55306/dvdrental';
const cookieParser = require("cookie-parser");

const client = new Client({
    connectionString: connectionString
});

client.connect();

const app = express();

app.use(express.json());

app.use(bodyParser.urlencoded({ extended: false }));

app.use(cookieParser("[mysecrethere]"));

app.use(session({
    secret: "Dog",
    resave: true,
    saveUninitialized: true,
}));


app.get('/', function (req, res) {

   client.query('SELECT * FROM actor', [1], function (err, result) {
              if (err) {
                console.log(err)
               } else {
                 console.log(result);
               }
     });
});

1 Answer 1

1

You should finalize your middleware to return the data from database. As an example, your code with minimal changes:

app.get('/', function (req, res) {
    client.query('SELECT * FROM actor', [1], function (err, result) {
          if (err) {
            console.log(err)
            return res.status(500).json(err.detail)
           }
           console.log(result);
           res.status(200).json(result.rows)
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

I tried finishing the middleware but it still not working.
Sorry it works now, I forgot that a while back I changed the Port Number to 5433.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.