Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'connect' of undefined #5733

Closed
stephanoparaskeva opened this issue Mar 21, 2020 · 18 comments
Closed

Cannot read property 'connect' of undefined #5733

stephanoparaskeva opened this issue Mar 21, 2020 · 18 comments

Comments

@stephanoparaskeva
Copy link

stephanoparaskeva commented Mar 21, 2020

Issue type:

[ ] question
[x ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Configuration Options

export const test: Pool = {
  default: {
    name: 'default',
    type: 'postgres',
    host: 'localhost',
    port: 5432,
    username: 'test',
    password: '',
    database: 'main_test',
    logging: false,
    synchronize: true,
    migrations: ['db/migration/**/*{.ts,.js}'],
    subscribers: ['db/subscriber/**/*{.ts,.js}'],
    entities: ['dist/**/*.entity{.ts,.js}', 'app/**/*.entity{.ts,.js}'],
    cli: {
      migrationsDir: 'db/migration',
      subscribersDir: 'db/subscriber',
      entitiesDir: '',
    },
  },
};

Code

import supertest from 'supertest';
import { createServer } from 'http';
import { createConnection, getConnection } from 'typeorm';
import { test } from '../../config/connections';
import app from '../../app';

describe('Category - Testing', () => {
  beforeEach(async () => createConnection(test.default));
  afterEach(async () => getConnection('default').close());

  const testServer = supertest(createServer(app));

  it('responds with status code 200', async () => {
    const x = async () => {
      try {
        await testServer.get('/categories');
      } catch (err) {
        console.log(err);
      }
    };
    expect(x).toThrow();
  });
});

Error

TypeError: Cannot read property 'connect' of undefined
        at /Users/Stephano/Development/code-basket/application_6374/src/driver/postgres/PostgresDriver.ts:774:25
        at new Promise (<anonymous>)
        at PostgresDriver.Object.<anonymous>.PostgresDriver.obtainMasterConnection (/Users/Stephano/Development/code-basket/application_6374/src/driver/postgres/PostgresDriver.ts:773:16)
        at PostgresQueryRunner.Object.<anonymous>.PostgresQueryRunner.connect (/Users/Stephano/Development/code-basket/application_6374/src/driver/postgres/PostgresQueryRunner.ts:90:58)
        at PostgresQueryRunner.<anonymous> (/Users/Stephano/Development/code-basket/application_6374/src/driver/postgres/PostgresQueryRunner.ts:163:55)
@tarcisiobruni
Copy link

Im having the same error when running test step in CI/CD from Gitlab.

@michaeldever
Copy link

Same error when running test step in Github actions.

@tarcisiobruni
Copy link

I think It may be an error caused by 'pg' lib package. Some days ago was made an new release. I'll try a downgrade with 7.x.x version

@michaeldever
Copy link

@tarcisiobruni Was this working previously in your project? I've just setup a new project, so unsure if it's something in that or something has changed for existing project.

@michaeldever
Copy link

I downgraded to7.18.2 and I still see the error.

@tarcisiobruni
Copy link

tarcisiobruni commented Apr 26, 2020

@michaeldever Yes, was working perfectly 5 days ago. Any positive behavior I'll report here. When I made my project lib dependencys in Package.Json, pg was ^7.12.x.

@michaeldever
Copy link

@tarcisiobruni Thanks for letting me know, I can hold off digging through my code until I know if it's an issue or not so. FWIW, I tried 7.14.0 as well and see the same issue.

@tarcisiobruni
Copy link

tarcisiobruni commented Apr 26, 2020

@michaeldever , what version of NodeJs is running while tests has been executed? I try to rerun some tests job that in past resulted in sucess, but now, a got error. If all was currectly before, so, whats now? I checked my Ci/Cd config in .gitlab-ci.yml, and when I saw how the Node version were being downloaded (node:latest), I got an Eureka! Node 14 has been released some days ago. Doing downgrade to 12.16.2, now all jobs looks going weel again. Try this too. Waiting your results...

@michaeldever
Copy link

@tarcisiobruni if we ever cross paths IRL, drinks are on me.

Changed to node:13-alpine and the tests run as expected. I had been on node:alpine which was pulling version node 14.

@rebolyte
Copy link

Same problem here, running Node 12.8.1.

@ctrlplusb
Copy link

I had the same issue and then managed to debug it down to the fact that I had some code that was disposing my connection prior to another TypeORM operation occurring. Worth looking at your code if this may be the culprit for some of you. The error message isn't exactly helpful in this regard.

@imnotjames
Copy link
Contributor

imnotjames commented Sep 30, 2020

Are y'all still have this issue in Typeorm 0.2.27? & with using pg 8.0.3?

@Sun1ive
Copy link

Sun1ive commented Jan 5, 2021

0.2.29 typeorm
8.5.1 pg

Same issue in runtime.
But i have the db connection established. queries continue to works

@LeoInCode
Copy link

LeoInCode commented Mar 31, 2021

I have was the same problem, and the reason is local path in the migrations. I fix it trying change the local path.

@dev-juju
Copy link

I had same problem. Turns out my connection was not even being established. Just added some logging and saw why the connection was failing. Turns out I was using wrong postgresql array syntax in one of my entities.
Fixed the syntax error, connection established successfully and connection error disappeared.

Verify your connection is actually successful or catch and fix possible errors

    try {
      await createConnection();
      console.log('Database connection established!');
    }
    catch (err) {
      console.error(err);
    }

@Ovyerus
Copy link

Ovyerus commented Jul 29, 2021

Getting this with typeorm@0.2.35, pg@8.7.1 and on both Node 14.17.3 and 12.22.3 (making sure it wasn't just a 14 error), and using Jest.

First couple of tests run fine, and then one errors with this, and then all the following tests run fine, but Jest doesn't exit because a TCPWRAP keeps it open, presumably from the failed connection. Been tearing my hair out trying to figure this out and I'm completely stuck.

@manisha-11-dev
Copy link

Are y'all still have this issue in Typeorm 0.2.27? & with using pg 8.0.3?

am still facing issue - 'TypeError: Cannot read property 'connect' of undefined'

versions am using ::

node - v12.20.1
npm - 6.14.10
pg - 8.7.1
typeorm - 0.2.16

please help as its really urgent. I am fixing a high priority issue by researching around whether typeorm support dynamic connection via fetching db creds from aws secrets manager. I have successfully fetched it but now am looking into how we can retry the connection in case of newly fetched values.

@imnotjames
Copy link
Contributor

You're probably not connected. 0.2.16 is pretty old.

#7995 Might help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests