Error: Unable to locate metadata for the entity "BusinessApplication"

I have been utilizing TypeORM smoothly for some time, but out of the blue, I encountered this error during an API call:

EntityMetadataNotFound: No metadata for "BusinessApplication" was found.
    at new EntityMetadataNotFoundError (C:\Users\Robbie\Code\fit-society\node_modules\typeorm\error\EntityMetadataNotFoundError.js:10:28)
    at Connection.getMetadata (C:\Users\Robbie\Code\fit-society\node_modules\typeorm\connection\Connection.js:336:19)
    at EntityManager.<anonymous> (C:\Users\Robbie\Code\fit-society\node_modules\typeorm\entity-manager\EntityManager.js:459:44)
    ...

<p>The issue arises when this piece of code is executed:</p>

<pre class="lang-js"><code>import { BusinessApplication } from '../../../backend/all-entities';
import db from '../../../backend/database';

// within a function...
      const manager = await db.getManager();
      // in this case, req.data.id does equal "oldest"
      const application: BusinessApplication | undefined =
      req.data.id === 'oldest'
          ? (await manager.find(BusinessApplication, { order: { dateSubmitted: 'DESC' }, take: 1 }))[0]
          : await manager.findOne(BusinessApplication, { where: { id: parseInt(req.data.id, 10) } });
      if (application == null) throw createError(404, 'Business application not found');
      return application;

In backend/all-entities.ts:

...

In backend/database.ts:

...

In backend/entities/BusinessApplication.ts:

...

Despite having everything seemingly set up correctly with imports and decorators in place, why am I still facing this error? Interestingly, changing

await manager.find(BusinessApplication, ...)
to
await manager.find('BusinessApplication', ...)
resolves the issue, but it sacrifices intellisense. Also, this error only occurs after a hot-module-reload by Webpack, typically triggered by changes in the codebase or page disposal in Next.js.

Answer №1

The issue

After implementing webpack hot-reload in my project, I encountered a problem where new entity models were being generated but not recognized by TypeORM. This happened because TypeORM only established a connection to the database once when the database.ts module was initialized. As a result, when comparing old and new entities using

manager.find(BusinessApplication, ...)
, TypeORM did not recognize them as the same due to JavaScript's lack of referential equality. Consequently, it failed to detect the updated metadata in
manager.connection.entityMetadatas
.

The solution

To resolve this issue, I simply needed to create a new connection to the database after each reload so that it could properly incorporate the new entity metadata.

Answer №2

To resolve the issue, delete the dist folder from your project directory and then try running it again.

Answer №3

Encountered this issue after changing the name of an entity without including .entity in the file name

Answer №4

Remember to include entities in your DataSource when using the latest version of TypeORM (0.3.0 and above)

https://example.com/updated-typeorm-docs

export const dataSource = new DataSource({
    ...
    ...
    entities: [Employee, Product],
    ...
    ...
})

Answer №5

Following some additional effort, I decided to delete the dist folder from my project and give it a test run.

Success! It functioned perfectly.

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

Angular 2 select does not recognize the selected option

In my Angular 2 code, I am using ngFor to populate a dropdown with options. I want a specific option at a certain index to be selected by default. Currently, I tried using [attr.selected]="i == 0" but it ends up selecting the last option instead of the fi ...

Having an issue in Angular 2 where the correct function is not triggered when a button is placed within a selectable anchor

Within an anchor element, I have a button that triggers its own click listener for an editing popup module. The anchor itself has another click listener assigned to it. For example: <a (click)="onClick(myId)" class="list-group-item clearfix"> < ...

Although server-side rendering is utilized with react-query, JSON data continues to be displayed in the network tab

As per my understanding, the only way to conceal a JSON return from an API call is through server rendering. I have implemented this on all pages using Next.js; however, the network tab still displays a JSON with my data on pages where useInfiniteQuery and ...

Delay the rendering of the fetching component until the fetch operation is completed

My task is to retrieve data for SSR and send that data from the Client. const MyPage = ({ myFetch1, myFetch2, myFetch3, }) => { const dispatch = useDispatch(); dispatch(doSomething1(myFetch1)); dispatch(doSomething2(myFetch2)); dispatch(do ...

What is the official name of the key type for the Built-in Object?

There was a built-in type that I used in the past which represented the union of all possible object keys. It was named objectKey or something similar. Here is an example: type objectKey = string | number | symbol Unfortunately, I am drawing a blank on t ...

Rapidly update code changes using the development mode in Next.js with the VS Code Remote Container/devcontainer

Struggling to enable Next.js' Fast Refresh feature while using a VS Code Remote Container. Running npm run dev displays the app on localhost, indicating the container functions properly - but Fast Refresh remains ineffective. Next.js version: v11.0.1 ...

Error TS2322: The specified type Login cannot be assigned to the given type

I've been facing an issue while working on my app in react native. The error message I keep encountering is as follows: TS2322: Type 'typeof Login' is not assignable to type ScreenComponentType<ParamListBase, "Login"> | undefined Type ...

Afterward, Vercel disconnects from Supabase, causing the connection to be dropped

Over the past month, there have been recurring issues with Vercel dropping the connection to Supabase on a daily basis. The support team at Supabase claims that the error ('user not found - and the auth tool is supabase/auth') is not related to ...

Transitioning an NX environment to integrate ESM

My NX-based monorepo is quite extensive, consisting of half a dozen apps, frontend, backend, and dozens of libs. Currently, everything is set up to use commonjs module types, as that's what the NX generators have always produced. However, many librar ...

Encountered an error during the production build of NEXTJS, where it panicked due to the global thread pool not being initialized

While hosting my app on Heroku and running git push heroku main, I encountered the following error: panicked at 'the global thread pool has not been initialized.: threadpool builderror { kind: ioerror(error { kind: unsupported, message: "operatio ...

Implementing Angular 4 to fetch information from a JSON file

As a beginner in Angular, my current task involves loading data from a JSON file upon click, which I have successfully achieved so far. However, I am facing an issue where I'm unable to load the first JSON object before clicking, meaning that I want ...

What is the best way to organize checkboxes (either checked or unchecked) within a mat-table?

https://i.stack.imgur.com/cDQY7.png <ng-container matColumnDef="scheduled"> <th mat-header-cell mat-sort-header *matHeaderCellDef> Scheduled </th> <td mat-cell *matCellDef="let station"> ...

When incorporating babel-plugin-styled-components, Nextjs may encounter a mismatch error with classnames

After transferring the content of _document.js from the following styled components example and implementing the babel plugin mentioned in the styled components documentation, I am still encountering an error. _document.js import Document from 'next/ ...

The nest build process encounters errors related to TypeScript in the @nestjs/config package, causing it

Encountering several issues related to @nestjs/config, causing the npm build command to fail. However, npm run start:dev is still functional despite displaying errors. See below for screenshots of the errors and environment: ...

Unsure about NextJS: Should I opt for the "app" directory or stick with the "pages" folder? And is it better to name my main file "index.tsx" or "page.tsx"?

I find NextJS to be quite confusing. Most tutorials and official examples I've come across, along with the documentation, suggest organizing the application using a pages directory for frontend routes and an optional api directory for backend services ...

creating a JSON array within a function

I am currently developing an Angular application and working on a component with the following method: createPath(node, currentPath = []){ if(node.parent !==null) { return createPath(node.parent, [node.data.name, ...currentPath]) } else { retu ...

Facing problem with implementing NgMoudleFactoryLoader for lazy loading in Angular 8

A situation arose where I needed to lazy load a popups module outside of the regular router lazy-loading. In order to achieve this, I made the following adjustments: angular.json "architect": { "build": { ... "options": { ... "lazyM ...

Can you please explain the process of sending an object to a different page using the useRouter method from 'next/navigation'?

Currently, I find myself in a situation where I need to redirect the user from one page to another and pass an object using the useRouter hook from the next/navigation package within the app directory. Can someone guide me on how to modify the code snippet ...

Best practice for incorporating Bootstrap into Webpack

Greetings everyone, I've been experimenting with Bootstrap for Webpack, but I've hit a roadblock. After reading numerous blog articles, I found that they either rely on the outdated 'bootstrap-webpack' plugin from 7 months ago (which d ...

Issue with Angular 8: discrepancy between the value utilized in component.html and the value stored in component.ts (Azure application service)

Encountering a peculiar behavior in one of my Angular applications. In the component.html file, I aim to display "UAT" and style the Angular mat elements with a vibrant orange color when in UAT mode, while displaying them in blue without any mention of UAT ...