简体   繁体   中英

Client.query not retrieving data in Node JS

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);
               }
     });
});

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)
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM