1

I am trying to set up a database connection from my existing react app. But I am getting net.Socket is not a constructor error. I have seen this Node JS: net.Socket is not a constructor but still can't figure out what should I do. I have tried using websocket but then I am getting some other exception -websocket is not a constructor.

Here is my code


import { Client } from 'pg';

const app = express();
const query = 'SELECT id, description, name, salary FROM public.usecase_details';
const client = new Client({
  user: 'out',
  host: 'pgn.example.com',
  database: 'salary_details',
  password: 'out',
  port: 1032,
});
client.connect();

client.query(query)
.then((res) => {
  console.log('Table is found');
})
.catch((err) => {
  console.error(err);
})
.finally(() => {
  client.end();
});

Can I please get some idea/ help to solve this issue?

here are the error details: enter image description here

2
  • can we the code of the client ? Commented Nov 25, 2020 at 20:57
  • pg (full name: node-postgres) is a library meant for usage in a node.js environment. You cannot use it in the browser with react. You need an application server between the website and the database. How are you serving your html and js files? Commented Nov 25, 2020 at 21:39

0

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.