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

TypeError: Cannot read property 'name' of undefined #154

Open
alfaben12 opened this issue Oct 5, 2021 · 7 comments
Open

TypeError: Cannot read property 'name' of undefined #154

alfaben12 opened this issue Oct 5, 2021 · 7 comments
Assignees

Comments

@alfaben12
Copy link

alfaben12 commented Oct 5, 2021

Screenshot from 2021-10-05 11 42 56

Hi I'am already following instruction on docs but I got this error

user.factory.ts

import * as Faker from "faker";
import { User } from "src/users/entities/user.entity";
import { define } from "typeorm-seeding";

define(User, (faker: typeof Faker) => {
  const gender = faker.random.number(1);
  const firstName = faker.name.firstName(gender);
  const lastName = faker.name.lastName(gender);

  const user = new User();
  user.name = `${firstName}  ${lastName}`;
  user.email = faker.internet.email();
  return user;
});

And

create-user.seed.ts

import { User } from "src/users/entities/user.entity";
import { Connection } from "typeorm";
import { Seeder, Factory } from "typeorm-seeding";

export class CreateUser implements Seeder {
  public async run(factory: Factory): Promise<void> {
    await factory(User)().create();
  }
}
@jorgebodega jorgebodega self-assigned this Oct 6, 2021
@jorgebodega
Copy link
Collaborator

Hi! Can you share how are you running the seeder? Just running nest build somehow? Could be useful, I cannot reproduce this.

BTW, I updated your issue to use markdown, more readable 👍

@harveysanders
Copy link

harveysanders commented Oct 6, 2021

@alfaben12 We just had this same error. It boiled down to bad paths passed to the ormconfig.js configuration. There is no validation on your factories paths, so if they're invalid, you'll end up with unregistered factories.
Ex:

module.exports = {
  ...
  seeds: ['src/seeds/**/*{.ts,.js}'],
  factories: ['src/factories/**/*{.ts,.js'],  // Note the missing closing "}"
}

Further Explanation:

Here entityFactory is undefined.

export const factory: Factory = <Entity, Context>(entity: ObjectType<Entity>) => (context?: Context) => {
  // ...                          entityFactory is undefined  vvvvv
  return new EntityFactory<Entity, Context>(name, entity, entityFactoryObject.factory, context)
}

It gets defined when define is called somewhere in your factories code. If the factories path in your configuration is invalid, your factories files are never run, and define is never called, so you end up with undefined entityFactoryObject

Hope that helps

@jorgebodega
Copy link
Collaborator

Please @alfaben12, could you check what @harveysanders said?

If that fixes your problem, I could try to do something with path validation (Maybe print files loaded with verbose flag?)

@harveysanders
Copy link

@alfaben12 I'm sorry I think I may have jumped the gun with your error. Our error was "Cannot read property 'factory' of undefined". I'm not sure it's related to your error.

Looking at your stack trace, I see your error is due to undefined seeds so it's possible your problem is similar.

const seedFiles = loadFiles(option.seeds)

Check your seeds path in your ormconfig file. Try throwing in a log in one of your seed files to ensure they're being loaded and executed.

@alfaben12
Copy link
Author

@harveysanders thank you for reference, my ormconfig is wrong that's why maybe that's why we get the same error
@jorgebodega I change seeder without build, if force with build will be error, let me little explanation in my case

ormconfig.json (Before)

    "entities": ["dist/**/*.entity{.ts,.js}"],
    "seeds": ["dist/**/*.factory{.ts,.js}"],
    "factories": ["dist/**/*.seed{.ts,.js}"],

ormconfig.json (After)

    "entities": ["src/**/*.entity{.ts,.js}"],
    "seeds": ["src/**/seeds/*{.ts,.js}"],
    "factories": ["src/**/factories/*{.ts,.js}"],

package.json (Before)

    "seed:config": "ts-node ./node_modules/typeorm-seeding/dist/cli.js config",
    "seed:run": "ts-node ./node_modules/typeorm-seeding/dist/cli.js seed"

package.json (After)

    "seed:config": "npm run build && ts-node -r tsconfig-paths/register ./node_modules/typeorm-seeding/dist/cli.js config",
    "seed:run": "npm run build && ts-node -r tsconfig-paths/register ./node_modules/typeorm-seeding/dist/cli.js seed"

Conclusion for ormconfig.json my bad is, I reverse for seed and factory and change entities from dist/ to src/ if you not change to src/ folder you will get error like this

EntityMetadataNotFoundError: No metadata for "User" was found.

Add -r tsconfig-paths/register in seed:config & seed:run again, if not added maybe the entity will not be found when run seed:run

@jorgebodega
Copy link
Collaborator

I'll check this with sample code, maybe docs could be updated with this info.

@kingsloob1
Copy link

# #

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

No branches or pull requests

4 participants