typeorm-seeding
typeorm-seeding copied to clipboard
TypeError: Cannot read property 'name' of undefined

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();
}
}
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:
@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
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?)
@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 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
I'll check this with sample code, maybe docs could be updated with this info.