w3tecch / typeorm-seeding

🌱 A delightful way to seed test data into your database.
https://www.npmjs.com/package/typeorm-seeding
MIT License
877 stars 131 forks source link

TypeError: Cannot read properties of undefined(reading 'number') #214

Open andi-faizal94 opened 2 years ago

andi-faizal94 commented 2 years ago

edited

I have entity:

Screen Shot 2022-03-28 at 17 54 28

user.factory.ts

Screen Shot 2022-03-27 at 22 26 55

I have problem with file user.factory.ts:

const gender = faker.random.number(1)

To solve my problem i change:

  1. Change column User entity:

    @PrimaryGeneratedColumn("uuid") id: string;

  1. Change in user.factory.ts :
    • const gender = faker.random.number(1)
    • const age = faker.random.number(1)

change with:

  1. Change In Package.json: "seed:run": "ts-node ./node_modules/typeorm-seeding/dist/cli.js seed"

change with:

"seed:run": "ts-node ./node_modules/typeorm-seeding/dist/cli.js --configName ./ormconfig.json seed"

this is work to me.

Thank you.

jorgebodega commented 2 years ago

Hi! Can you just put some more explicit example, or maybe create a repo to reproduce this? Just because you talked about gender but I cannot see that on entity

andi-faizal94 commented 2 years ago

Hi! Can you just put some more explicit example, or maybe create a repo to reproduce this? Just because you talked about gender but I cannot see that on entity

Hi I'm sorry about it:

my case in Entity: import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";

@Entity() export class User extends BaseEntity { @PrimaryGeneratedColumn() id: number;

@Column() firstName: string;

@Column() lastName: string;

@Column() age: number;

@Column({ nullable: true }) addres: string; }

And I have a file user.factory.ts :

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

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

const user = new User(); user.firstName = firstName; user.lastName = lastName; user.age = age; user.addres = addres; return user; });

I fix with :

Change column User entity: @PrimaryGeneratedColumn("uuid") id: string;

Change faker.random.number: const gender = faker.random.uuid(); const age = faker.random.number({ min: 10, max: 20 }); Change In Package.json: "seed:run": "ts-node ./node_modules/typeorm-seeding/dist/cli.js seed" change with:

"seed:run": "ts-node ./node_modules/typeorm-seeding/dist/cli.js --configName ./ormconfig.json seed"

this is work to me.

My Repository:

https://github.com/andi-faizal94/RESTfull-API-TYPEORM-Express-PostgreSQL/blob/main/src/database/factories/user.factory.ts

richoandika commented 2 years ago

@andi-faizal94 Hi, I think there's your problem bro image just adding ./ into path of module like import { UserEntity } from './../entities/user.entity'