typeorm-seeding icon indicating copy to clipboard operation
typeorm-seeding copied to clipboard

TypeError: Cannot read property 'name' of undefined

Open alfaben12 opened this issue 4 years ago • 7 comments

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();
  }
}

alfaben12 avatar Oct 05 '21 04:10 alfaben12

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 :+1:

jorgebodega avatar Oct 06 '21 20:10 jorgebodega

@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

harveysanders avatar Oct 06 '21 22:10 harveysanders

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?)

jorgebodega avatar Oct 06 '21 23:10 jorgebodega

@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. https://github.com/w3tecch/typeorm-seeding/blob/b97290ca0b13e3d876dd8c7ab6da8341b447a9c2/src/commands/seed.command.ts#L69

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.

harveysanders avatar Oct 07 '21 01:10 harveysanders

@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

alfaben12 avatar Oct 07 '21 02:10 alfaben12

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

jorgebodega avatar Oct 07 '21 09:10 jorgebodega

# #

kingsloob1 avatar Apr 26 '22 08:04 kingsloob1