TypeError: net.Socket is not a constructor
See original GitHub issueThe task: Connect the Postgresql 11 database to the project on React to get data from there.
What I did to connect to the database: Created a new React project:
create-react-app PGTestProject
Then I tried to connect to the database. The actions listed in the “Getting started” section are not enough to get started with the package. For example, it does not indicate how to change the connection parameters to the database. Without this, it is impossible to connect to the created database.
The actions listed in the “Connecting” section are also not enough. The error “net.Socket is not a constructor”
App.js:
import React from 'react';
import logo from './logo.svg';
import './App.css';
async function getDatafromDB(){
const { Pool, Client } = require('pg')
const pool = new Pool({
user: 'postgres',
host: 'server_ip',
database: 'mydb',
password: 'password',
port: 5432,
})
pool.query('SELECT NOW()', (err, res) => {
console.log(err, res)
pool.end()
})
const pool = new Pool({
user: 'postgres',
host: 'server_ip',
database: 'mydb',
password: 'password',
port: 5432,
})
client.connect()
client.query('SELECT NOW()', (err, res) => {
console.log(err, res)
client.end()
})
}
function App() {
getDatafromDB();
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo"/>
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
An error occurs in the browser console: TypeError: net.Socket is not a constructor
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7
Top Results From Across the Web
Node JS: net.Socket is not a constructor - Stack Overflow
I've tried making the below code an object and importing / requiring it into my project and I still get that error. var...
Read more >Unable to connect to the database: TypeError: net.Socket is ...
i'm trying to connect to a postgres database within my node.js-vue.js app , but in console I'm getting this error:
Read more >TypeError: net.Socket is not a constructor - PostgreSQL
Re: Unable to connect to the database: TypeError: net.Socket is not a constructor. From: Marco Ippolito <ippolito(dot)marco(at)gmail(dot) ...
Read more >TypeError: "x" is not a constructor - JavaScript - MDN Web Docs
The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor,...
Read more >Errors | Node.js v19.3.0 Documentation
Usually encountered by http or net . Often a sign that a socket.end() was not properly called. Class: TypeError #.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can’t run pg in the browser. It runs in Node.
@micimize Cloudflare workers don’t run in the browser, as far as I know.