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

The Angular Animation constantly resets with each new action taken

In my Angular project, I am working on a scaling animation for a list. I want the animation to only trigger when specific buttons (red and green) are pressed. Currently, the animation restarts regardless of what I click on. Can anyone help me troubleshoot ...

Error: The specified type 'OmitWithTag<GetServerSidePropsContext<ParsedUrlQuery>, keyof PageProps, "default">' does not meet the requirements of '{ [x: string]: never; }'

While my app works in dev mode, it crashes when I try to run the build command. The output of npm run build is as follows: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5dbd0cdc198d4c5c598c1d0d8c5d9d4c1d0f5859b859b84"&g ...

The Angular Material Table is reporting an error: the data source provided does not conform to an array, Observable, or DataSource

Having some trouble with an Angular Material table, as I'm encountering an error preventing me from populating the grid: Error: Provided data source did not match an array, Observable, or DataSource search.service.ts GridSubmittedFilesList: IGridMo ...

When working with Typescript and Vue.js, it's important to ensure that properties are initialized before

Check out the following code snippet: export default class PrimitiveLink extends Vue { style = { // Reset display: 'inline-block', textDecoration: 'none', outline: 'none', // Theme ...this.themeStyle ...

Tips on changing the date format in Typescript to the desired format

My date string reads as 2016-09-19T18:10:31+0100. Here's what I'm doing: let dateString:string = 2016-09-19T18:10:31+0100; let newDateString:Date = new Date(dateString); The output I'm currently getting is Tue Sep 19 2016 18:10:31 GMT+0530 ...

Is it possible to incorporate FCM into Next.js and effectively handle server-side events for FCM on Next.js servers?

Is it feasible to incorporate the Firebase Cloud Messaging (FCM) into my upcoming Next.js application? Can I manage both client-side and server-side modules within the server side? ...

What is it about Kyle Simpson's OLOO methodology that seems to swim against the tide of Typescript's popularity?

Disclaimer: this post might come across as impulsive. Warning for Typescript beginners! Also, a bit of a vent session. Recently, I delved into the OLOO approach from the YDKJS book series within a Typescript and Node environment. // ideal JS syntax le ...

Slate - developing a TypeScript function to filter and retrieve an object containing the highest property value

Check out this NEW RELATED QUESTION: I need to extract the largest number from a given object set. I am struggling with finding a solution. I have tried using max but I think my skills are lacking. Here is the code I have so far: @Function() pub ...

When using Angularfire, the function to switch the type from snapshotChanges will consistently return the value as "value"

At this moment, when I use the Angularfire extension to call the following code: this.db.doc(path).snapshotChanges(); Angularfire always retrieves a DocumentSnapshot with a type that is consistently "value", regardless of the actual change type. Is there ...

On MacOS, VSCode encounters an issue with updating TypeScript imports when files are moved

I recently started transitioning a project from a mixed JS/TS setup to fully TypeScript. The project's server is hosted on AWS Lambda, and I have a tsconfig file at the root level as well as one in the /lambdas directory. One issue I've encounte ...

At what point is it ideal to initiate a project?

Imagine my project as an npm package where I use tools like webpack or grunt to bundle my css, js, and img files into a dist directory. The build script is specified in the package.json. When is the best time to execute the build script? Should I do it du ...

Set an interface to null within Angular 4

I've created an interface in Angular 4 called StatusDetail: interface StatusDetail { statusName: string, name: string } Next, I assigned some values to it within an Angular component: //Angular Component export class EditComponent implemen ...

Angular 5 is throwing an error that says: "There is a TypeError and it cannot read the property 'nativeElement' because it

Being aware that I may not be the first to inquire about this issue, I find myself working on an Angular 5 application where I need to programmatically open an accordion. Everything seems to function as expected in stackblitz, but unfortunately, I am enco ...

Refreshing issue: Model change in child page not updating view correctly (Ionic & Angular)

I am currently working with Ionic 3.20 and Angular 5.2.9, encountering an issue with content refreshing after a model change. Despite being new to this, I sense that I might be overlooking something fundamental. Within my view, I have the following elemen ...

Creating a multilingual website with Nextjs 13 App Router using internalization, all without the need

As I develop a web app that requires user authentication to access, I am currently using Next.js 13 with the App Route (not Pages Route). My goal is to implement internationalization without having to create sub-routes like /en, or use domains such as mywe ...

Next.js 14.0.4 is encountering an ENOENT error due to the absence of the BUILD_ID file

My Next.js 14.0.4 project is running into an ENOENT error due to the missing BUILD_ID file, here are some specifics about my project: Node.js version: 21.5.0 Next.js version: 14.0.4 [email protected] start next start ▲ Next.js 14.0.4 Local: ...

Looking to create a website that renders the page after successfully retrieving the data in Next.js

As a newcomer to Next.js with some knowledge in JavaScript, I am trying to fetch data from an API using the code snippet below. The goal is to display this data on the /blogs webpage: import React from 'react' const blogs = () => { var eleme ...

Encountering difficulties initiating NextJS with Nginx

After creating a fresh project using Next JS, I successfully accessed it through the browser using an IP and port like "http://127.0.0.1:4321/". You can view the attached image here. However, when attempting to connect to the project via Nginx with an IP ...

What is causing the warnings for a functional TypeScript multidimensional array?

I have an array of individuals stored in a nested associative array structure. Each individual is assigned to a specific location, and each location is associated with a particular timezone. Here is how I have defined my variables: interface AssociativeArr ...

Breaking down this.props and assigning it to Component

In the example code provided below, we are working with React and Next.js import App, { Container} from "next/app"; import React from "react"; class MyApp extends App{ render() { const { Component } = this.props; re ...