Re: Unable to connect to the database: TypeError: net.Socket is not a constructor

From: Marco Ippolito <ippolito(dot)marco(at)gmail(dot)com>
To: Tim Cross <theophilusx(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Unable to connect to the database: TypeError: net.Socket is not a constructor
Date: 2020-04-21 07:34:59
Message-ID: CAFegzBTy2p4CEoprJrm4Oin3h3oshAVXJUVgkgWVky_moA-C7Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thank you very much Tim and Adrian for your very kind and valuable
information and suggestions.

Marco

Il giorno mar 21 apr 2020 alle ore 00:47 Tim Cross <theophilusx(at)gmail(dot)com>
ha scritto:

>
> Marco Ippolito <ippolito(dot)marco(at)gmail(dot)com> writes:
>
> > I'm trying to connect to a postgres database (Postgresql-11) within my
> > nodejs-vue.js app, but in console I'm getting this error:
> >
> > [HMR] Waiting for update signal from WDS...
> > pg.js?c8c2:27 Unable to connect to the database: TypeError:
> net.Socket
> > is not a constructor
> > at new Connection
> > (webpack-internal:///./node_modules/pg/lib/connection.js:22:34)
> > at new Client
> > (webpack-internal:///./node_modules/pg/lib/client.js:55:37)
> > at Promise.tap.query
> > (webpack-internal:///./node_modules/sequelize/lib/dialects/postgres
> > /connection-manager.js:124:26)
> > at ConnectionManager.connect
> > (webpack-internal:///./node_modules/sequelize/lib/dialects
> > /postgres/connection-manager.js:121:12)
> > at eval
> >
> (webpack-internal:///./node_modules/sequelize/lib/dialects/abstract/connection-
> > manager.js:318:50)
> > From previous event:
> > at ConnectionManager._connect
> > (webpack-internal:///./node_modules/sequelize/lib/dialects
> > /abstract/connection-manager.js:318:8)
> > at ConnectionManager.getConnection
> > (webpack-internal:///./node_modules/sequelize/lib/dialects
> > /abstract/connection-manager.js:254:46)
> > at eval
> > (webpack-internal:///./node_modules/sequelize/lib/sequelize.js:640:36)
> > From previous event:
> > at eval
> > (webpack-internal:///./node_modules/sequelize/lib/sequelize.js:631:53)
> > at eval
> > (webpack-internal:///./node_modules/retry-as-promised/index.js:70:21)
> > at new Promise (<anonymous>)
> > at retryAsPromised
> > (webpack-internal:///./node_modules/retry-as-promised/index.js:60:10)
> > at eval
> > (webpack-internal:///./node_modules/sequelize/lib/sequelize.js:631:30)
> > From previous event:
> > at Sequelize.query
> > (webpack-internal:///./node_modules/sequelize/lib/sequelize.js:580:23)
> > at Sequelize.authenticate
> > (webpack-internal:///./node_modules/sequelize/lib/sequelize.js:892:17)
> > at eval (webpack-internal:///./src/plugins/db/pg.js:23:11)
> > at Object../src/plugins/db/pg.js (
> https://ggc.world/js/app.js:1128:1
> > )
> > at __webpack_require__ (https://ggc.world/js/app.js:785:30)
> > at fn (https://ggc.world/js/app.js:151:20)
> > at eval (webpack-internal:///./src/main.js:16:72)
> > at Module../src/main.js (https://ggc.world/js/app.js:1083:1)
> > at __webpack_require__ (https://ggc.world/js/app.js:785:30)
> > at fn (https://ggc.world/js/app.js:151:20)
> > at Object.1 (https://ggc.world/js/app.js:1141:18)
> > at __webpack_require__ (https://ggc.world/js/app.js:785:30)
> > at checkDeferredModules (https://ggc.world/js/app.js:46:23)
> > at https://ggc.world/js/app.js:861:18
> > at https://ggc.world/js/app.js:864:10
> >
> > In /src/main.js :
> >
> > import '@/plugins/db/pg';
> >
> > (base) /src/plugins/db$ ls -lah
> > total 28K
> > drwxr-xr-x 6 marco marco 4,0K apr 20 15:42 .
> > drwxr-xr-x 3 marco marco 4,0K apr 20 15:41 ..
> > drwxr-xr-x 2 marco marco 4,0K apr 20 17:20 config
> > drwxr-xr-x 2 marco marco 4,0K apr 20 15:48 migrations
> > drwxr-xr-x 2 marco marco 4,0K apr 20 15:48 models
> > -rw-r--r-- 1 marco marco 819 apr 20 08:53 pg.js
> > drwxr-xr-x 2 marco marco 4,0K apr 20 17:21 seeders
> >
> >
> > nano pg.js
> >
> > const { Pool } = require('pg');
> > const { Sequelize } = require('sequelize');
> > const pool = new Pool();
> > const sequelize = new Sequelize('pusers', 'postgres', 'pwd', {
> > host: 'localhost',
> > dialect: 'postgres',
> > pool: {
> > max: 5,
> > min: 0,
> > acquire: 30000,
> > idle: 10000
> > }
> > });
> > sequelize
> > .authenticate()
> > .then(() => {
> > console.log('Connection has been established successfully.');
> > })
> > .catch(err => {
> > console.log('Unable to connect to the database:', err);
> > });
> >
> > module.exports = {
> > query: (text, params, callback) => {
> > return pool.query(text, params, callback);
> > },
> > }
> >
> > Populated the sample database with a row:
> >
> > pusers=# SELECT schemaname,relname,n_live_tup
> > pusers-# FROM pg_stat_user_tables
> > pusers-# ORDER BY n_live_tup DESC;
> > schemaname | relname | n_live_tup
> > ------------+---------------+------------
> > public | pusers | 1
> > public | SequelizeMeta | 1
> > (2 rows)
> >
> > I read here:
> >
> https://stackoverflow.com/questions/40599069/node-js-net-socket-is-not-a-constructor
> > that
> > "there are no plain TCP sockets in the browser, so that is why trying to
> > use `net.Socket` in the browser (via webpack, browserify, etc.) won't
> work"
> >
> > Environment Info:
> >
> > System:
> > OS: Linux 5.3 Ubuntu 18.04.4 LTS (Bionic Beaver)
> > CPU: (8) x64 Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
> > Binaries:
> > Node: 12.15.0 - ~/.nvm/versions/node/v12.15.0/bin/node
> > Yarn: 1.22.4 - ~/.nvm/versions/node/v12.15.0/bin/yarn
> > npm: 6.14.4 - ~/.nvm/versions/node/v12.15.0/bin/npm
> > Browsers:
> > Chrome: 81.0.4044.92
> > Firefox: 75.0
> > npmGlobalPackages:
> > @vue/cli: 4.2.3
> >
> > So... how to solve the problem?
> > Looking forward to your kind help.
> > Marco
>
> This has nothing to do with postgres. This is a purely Javascript issue
> combined with differences between the browser Javascript API and node
> javascript API.
>
> Bottom line - if your trying to connect directly to a PG database from
> within Javascript code running inside the browser, it won't work. The
> browser does not have the 'net' library. You can only do this from
> within node.js. Likewise, references to express are also red hearings -
> you don't have a web server inside the browser either. If your not
> trying to develop inside the browser but are in fact developing at the
> node.js level, then you don't need webpack.
>
> I think what you really need to do is step back and look closer at your
> architecture. Typically, you would put all your database interaction
> stuff in the web server using node.js. A common design pattern would be
> to use one of the node.js web servers, like express (but there are
> others) and have something like nginx as a proxy server in front of it.
> You would then wrap your database interaction in a node.js API which you
> would then call from your client browser using http/https or web
> sockets.
>
> --
> Tim Cross
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Олег Самойлов 2020-04-21 08:58:21 a prefix searching
Previous Message Tim Cross 2020-04-20 22:47:11 Re: Unable to connect to the database: TypeError: net.Socket is not a constructor