``Error Message: TypeORM - could not establish database connection

I encountered an issue while running my project built with Typescript, Typeorm, and Express. The error message received when running the dev script was:

connectionNotFoundError: Connection "default" was not found

The content of my ormconfig.json file is as follows:

{
  "name": "default",
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "postgres",
  "password": "docker",
  "database": "clean-node-api",
  "synchronize": true,
  "logging": true,
  "entities": [
    "./src/infra/typeorm/entities/*.ts"
  ],
  "cli": {
    "migrationsDir": "./src/infra/typeorm/migrations"
  }
}

You can find the source code in this repository: https://github.com/thuram/typescript-api-with-tdd

Answer №1

After cloning your repository and running the code, I encountered a backtrace:

ConnectionNotFoundError: Connection "default" was not found.
    at new ConnectionNotFoundError (/Users/lithium/develop/typescript-api-with-tdd/src/error/ConnectionNotFoundError.ts:8:9)
    at ConnectionManager.get (/Users/lithium/develop/typescript-api-with-tdd/src/connection/ConnectionManager.ts:40:19)
    at Object.getRepository (/Users/lithium/develop/typescript-api-with-tdd/src/index.ts:287:35)
    at new UserRepository (/Users/lithium/develop/typescript-api-with-tdd/src/infra/typeorm/repositories/UserRepository.ts:14:26)
    at Object.exports.makeCreateUser (/Users/lithium/develop/typescript-api-with-tdd/src/main/factories/useCases/user/CreateUserFactory.ts:5:26)
    at Object.exports.makeSignUpController (/Users/lithium/develop/typescript-api-with-tdd/src/main/factories/controllers/user/SignUpControllerFactory.ts:6:31)
    at Object.<anonymous> (/Users/lithium/develop/typescript-api-with-tdd/src/main/routes/SignUp.ts:5:26)
    at Module._compile (internal/modules/cjs/loader.js:1201:30)
    at Module._compile (/Users/lithium/develop/typescript-api-with-tdd/node_modules/source-map-support/source-map-support.js:547:25)
    at Module.m._compile (/private/var/folders/fx/7f3mc02s4lxbzjmzvw4cmknh0000gn/T/ts-node-dev-hook-1775948954112352.js:57:33)
[ERROR] 10:41:29 ConnectionNotFoundError: Connection "default" was not found.

The error originated from UserRepository.ts:14:26),

  constructor() {
    this.ormRepository = getRepository(User);
  }

It seems that you are attempting to create a repository or manager without establishing the connection. Placing the statement

const repo = await getRepository(User);
within a function should resolve this issue!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error encountered in Angular2: Attempted to access property 'compilerOptions' which is undefined

I encountered a TypeError: Unable to access the 'compilerOptions' property of undefined Below is the snippet of my compilerOptions code: { "compilerOptions": { "target": "ES5", "module": "commonjs", "emitDecoratorMetadata": tr ...

I'm curious why my express route functions properly when I write it first, but throws a cast error if I place it elsewhere in

One of the routes in my app involves a friend route that I have registered on the user route. The user route is also registered in the app. In the user schema, I have utilized mongoose.Types.ObjectId. Mongoose Version: 5.9.19 Within User.js: router.use ...

When working with Visual Studio and a shared TypeScript library, you may encounter the error message TS6059 stating that the file is not under the 'rootDir'. The 'rootDir' is expected to contain all source files

In our current setup with Visual Studio 2017, we are working on two separate web projects that need to share some React components built with TypeScript. In addition, there are common JavaScript and CSS files that need to be shared. To achieve this, we hav ...

Creating an enum in TypeScript can be accomplished by using the enum

What transformations do enums undergo during runtime in the TypeScript environment? Fruit.ts enum Fruit {APPLE, ORANGE}; main.ts let basket = [Fruit.APPLE, Fruit.ORANGE]; console.log(basket); The resulting main.js file remains identical to the .ts ver ...

Tips for creating Junit tests for a CDK environment

As a newcomer to CDK, I have the requirement to set up SQS infrastructure during deployment. The following code snippet is working fine in the environment: export class TestStage extends cdk.Stage { constructor(scope: cdk.Construct, id: string, props: ...

Issue with expressjs middleware not receiving req.params as expected

Similar to this I have set up the following route: app.get('/blogpost/:link', middleware.loginCheck, blog.postBlogs); When trying to access req.params.link within the middleware.loginCheck, it returns as undefined. It seems like the link para ...

Tips for obtaining the output of an asynchronous function that contains a query within a loop

I am facing an issue where I need to retrieve a value after the completion of a for loop that is nested within an asynchronous function. The loop contains a query that inserts data into a database. The function seems to be functioning correctly, but when ...

Retrieve data that resets to undefined upon reloading from an array

Encountering an unusual error while working with TypeScript for the first time. Initially, when I use console.log(data), it displays an array with objects. However, upon reloading the webpage without making any changes, the console log shows undefined. con ...

Guide on linking an Angular2+ app with an external API

Can anyone provide guidance on how to integrate an external API with authentication (username and password) into an Angular Application? I am comfortable connecting to APIs that don't require authentication, but I am facing difficulties with APIs that ...

Retrieving Data from DynamoDBClient by Utilizing a Global Secondary Index (GSI) that is Indexed by DeviceID and Timestamp

Currently, I have a Lambda function that serves as an Express.js server and API. All endpoints are being hit correctly and data is returned as expected, except for one function that should return a list of items in the specified format: [ { "id" ...

Error in navigating Node.js path

Within my server.js file, I have the following code: app.use(express.static(__dirname + '/public')); app.get('/', function(req, res){ res.sendFile('index.html'); }); app.get('/report', function(req, res){ ...

What is the best method for integrating JavaScript into my Express.js application that is also utilizing Socket.IO?

In order to synchronize time for my countdown project, I am utilizing the timesync package. server.js const app = require('express')(); const http = require('http').Server(app); const io = require('socket.io')(http); const po ...

What is the reason that {a: never} is not the same as never?

Is there a reason the code {a: never} cannot be simplified to just never? I believe this change would resolve the issues mentioned below. type A = {tag: 'A', value: number} type B = {tag: 'B', value: boolean} type N = {tag: never, valu ...

Issue: The variable 'HelloWorld' has been declared but not utilized

Why am I encountering an error when using TypeScript, Composition API, and Pug templating together in Vue 3? How do I resolve this issue when importing a component with the Composition API and using it in a Pug template? ...

Is Intellisense within HTML not available in SvelteKit when using TypeScript?

Having trouble with intellisense inside HTML for a simple page component. Also, renaming properties breaks the code instead of updating references. Typescript version: 4.8.4 Below is the code snippet: <script lang="ts"> import type { Bl ...

Encountering HTML content error when attempting to log in with REST API through Express on Postman

I'm currently in the process of developing a basic login API using Node.js and Express. However, I've encountered an error when testing with Postman: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

Unable to store object data within an Angular component

This is a sample component class: export class AppComponent { categories = { country: [], author: [] } constructor(){} getOptions(options) { options.forEach(option => { const key = option.name; this.categories[k ...

What is the process of including items in an Array?

I have been attempting to use the push method to add elements to an Array in Typescript, but strangely it doesn't seem to be working. The array just stays empty. Here's the code I have: list: Array<int> = Array(10) for(le ...

Connect the names of the sheets with the data in the tables

I have a simple question: I want to connect specific sheet names in my workbook with a table that contains a range of dates. The sheet names should be something like "blablabla" + Table@1. Although I have attempted to design a solution, it doesn't se ...

Guide to setting up .env Variables on a DigitalOcean Ubuntu droplet

After deploying my Node.js app on a DigitalOcean Ubuntu droplet, I encountered the need for variables from my .env file. How can I go about creating these variables within the DigitalOcean droplet? ...