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: str.replace is not a function in util/StringUtils.ts #2719

Closed
sinner opened this issue Aug 28, 2018 · 13 comments
Closed

TypeError: str.replace is not a function in util/StringUtils.ts #2719

sinner opened this issue Aug 28, 2018 · 13 comments

Comments

@sinner
Copy link

sinner commented Aug 28, 2018

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I've created an entity, so my User entity has the following structure:

import {
    Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne,
    BeforeInsert, BeforeUpdate, JoinColumn
} from "typeorm";

import {Contains, IsInt, Length, IsEmail, IsFQDN, IsDate, Min, Max, MaxLength, MinLength} from "class-validator";

/**
 * User Entity
 */
@Entity()
export class User {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({
        name: "uuid",
        type: "varchar",
        length: 150,
        unique: true
    })
    uuid: string;

    @Column({
        name: "email",
        type: "varchar",
        length: 180,
        unique: true
    })
    @IsEmail()
    @MaxLength(180, {
        message: "Email is too long"
    })
    email: string;

    @Column({
        name: "username",
        type: "varchar",
        length: 50,
        unique: true
    })
    @MinLength(3, {
        message: "Username is too short"
    })
    @MaxLength(50, {
        message: "Username is too long"
    })
    username: string;

    @Column({
        name: "password",
        type: "varchar",
        length: 150
    })
    password: string;

    @Column({
        name: "salt",
        type: "varchar",
        length: 150
    })
    salt: string;

    @Column({
        name: "first_name",
        type: "varchar",
        length: 150,
        nullable: true
    })
    @MaxLength(150, {
        message: "First name is too long"
    })
    firstName: string;

    @Column({
        name: "last_name",
        type: "varchar",
        length: 150,
        nullable: true
    })
    @MaxLength(150, {
        message: "Last name is too long"
    })
    lastName: string;

    @Column({
        name: "display_name",
        type: "varchar",
        length: 150,
        nullable: true
    })
    @MaxLength(150, {
        message: "Display name is too long"
    })
    displayName: string;

    @Column({
        name: "activation_code",
        type: "varchar",
        length: 150
    })
    activationCode: string;

    @Column({
        name: "roles",
        type: "simple-array"
    })
    roles: string[];

    @Column({
        name: "created_at",
        type: "timestamp"
    })
    @CreateDateColumn()
    createdAt: Date;

    @ManyToOne(type => User)
    @JoinColumn({ name: "created_by_id" })
    createdBy: User;

    @Column({
        name: "updated_at",
        type: "timestamp"
    })
    @UpdateDateColumn()
    updatedAt: Date;

    @ManyToOne(type => User)
    @JoinColumn({ name: "updated_by_id" })
    updatedBy: User;

    @Column({
        name: "is_email_confirmed",
        type: "boolean"
    })
    isEmailConfirmed: boolean;

    @Column({
        name: "is_active",
        type: "boolean"
    })
    isActive: boolean;

    /**
     *
     * @param {string} email
     * @param {string} username
     * @param {string} password
     * @param {string} salt
     * @param {string} displayName
     * @param {string} activationCode
     * @param {string[]} roles
     * @param {number} isActive
     */
    constructor(displayName?: string, 
                email?: string, 
                username?: string, 
                password?: string, 
                salt?: string, 
                activationCode?: string, 
                roles?: string[], 
                isEmailConfirmed?: boolean, 
                isActive?: boolean) {
        this.email = email;
        this.username = username;
        this.password = password;
        this.salt = salt;
        this.displayName = displayName;
        this.activationCode = activationCode;
        this.roles = roles;
        this.isEmailConfirmed = isEmailConfirmed;
        this.isActive = isActive;
    }

    @BeforeInsert()
    beforeCreate() {
        this.createdAt = new Date();
        this.updatedAt = this.createdAt;
    }

    @BeforeUpdate()
    beforeUpdate() {
        this.updatedAt = new Date();
    }

}

When I tried to generate the proper migration for it running:

ts-node ./node_modules/.bin/typeorm migration:generate -n

I'm receiving this response:

node@133cd09eb3d4:/var/srv/app$ ts-node ./node_modules/.bin/typeorm migration:generate -n
Error during migration generation:
TypeError: str.replace is not a function
    at Object.camelCase (/var/srv/app/src/util/StringUtils.ts:9:15)
    at Function.MigrationGenerateCommand.getTemplate (/var/srv/app/src/commands/MigrationGenerateCommand.ts:118:16)
    at Object.<anonymous> (/var/srv/app/src/commands/MigrationGenerateCommand.ts:88:62)
    at step (/var/srv/app/node_modules/typeorm/commands/MigrationGenerateCommand.js:32:23)
    at Object.next (/var/srv/app/node_modules/typeorm/commands/MigrationGenerateCommand.js:13:53)
    at fulfilled (/var/srv/app/node_modules/typeorm/commands/MigrationGenerateCommand.js:4:58)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)
@sinner
Copy link
Author

sinner commented Aug 28, 2018

If I modify the util/StringUtils.js in this way:

screen shot 2018-08-28 at 12 41 36 am

I receive this response:

node@133cd09eb3d4:/var/srv/app$ ts-node node_modules/.bin/typeorm migration:generate -n
User
true
Migration /var/srv/app/src/entity/migrations/1535434871947-true.ts has been generated successfully.

@sinner
Copy link
Author

sinner commented Aug 28, 2018

This true value looks realy weird...

@sinner
Copy link
Author

sinner commented Aug 28, 2018

I've realized that if I give a name after -n to the file, this error doesn't appear, but I think it's bad.

@gintsgints
Copy link

@sinner you have to specify name of migration you want to create.
https://github.com/typeorm/typeorm/blob/master/docs/migrations.md

typeorm migration:generate -n PostRefactoring

pleerock added a commit that referenced this issue Oct 27, 2018
FIX #2719 check provided name and give feedback if not.
@dgreene1
Copy link

It would be great to have a more helpful error message

@aldarund
Copy link

that should be handled and a proper message that migration name required should be provided instead of cryptic str replace error

@afzalIbnSH
Copy link

afzalIbnSH commented Jun 2, 2020

that should be handled and a proper message that migration name required should be provided instead of cryptic str replace error

Any plan to do this ?

@nitaking
Copy link

nitaking commented Sep 1, 2020

Why isn't anyone tackling this problem? Not nice at all.

@akosasante
Copy link
Contributor

I have a fairly simple fix for this; will put up a PR shortly once I figure out how to run tests locally for this repo 😅

akosasante pushed a commit to akosasante/typeorm that referenced this issue Sep 9, 2020
enforce the type of the name argument in order to return a more useful error message when the user forgets to provide a migration name to the generate migration command

Closes: typeorm#2719, typeorm#4798, typeorm#4805
akosasante pushed a commit to akosasante/typeorm that referenced this issue Sep 9, 2020
…ssing name argument

enforce the type of the name argument in order to return a more useful error message when the user forgets to provide a migration name to the generate migration command

Closes: typeorm#2719, typeorm#4798, typeorm#4805
pleerock pushed a commit that referenced this issue Sep 26, 2020
* fix: enforce name argument of migration generate command

enforce the type of the name argument in order to return a more useful error message when the user forgets to provide a migration name to the generate migration command

Closes: #2719, #4798, #4805

* fix: update error message text for generate migration command when missing name argument

enforce the type of the name argument in order to return a more useful error message when the user forgets to provide a migration name to the generate migration command

Closes: #2719, #4798, #4805

Co-authored-by: Akosua Asante <akosuaasante@gmail.com>
zaro pushed a commit to zaro/typeorm that referenced this issue Jan 12, 2021
… (typeorm#6690)

* fix: enforce name argument of migration generate command

enforce the type of the name argument in order to return a more useful error message when the user forgets to provide a migration name to the generate migration command

Closes: typeorm#2719, typeorm#4798, typeorm#4805

* fix: update error message text for generate migration command when missing name argument

enforce the type of the name argument in order to return a more useful error message when the user forgets to provide a migration name to the generate migration command

Closes: typeorm#2719, typeorm#4798, typeorm#4805

Co-authored-by: Akosua Asante <akosuaasante@gmail.com>
@navin-a11y
Copy link

Hi I was facing the same issue while creating any new migration file. And it's resolved now after adding below lines of code in "util/StringUtils.js" file.

if(typeOf str != "String){
return '';
}

@erald14
Copy link

erald14 commented Aug 6, 2021

Dont edit the source code. Just use the command as suggested

@romain130492
Copy link

any news on this?

@sangimed
Copy link

sangimed commented Apr 5, 2024

I've realized that if I give a name after -n to the file, this error doesn't appear, but I think it's bad.

Indeed, the error message is a bit confusing and misleading, which makes it difficult to identify and resolve the underlying problem.

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

No branches or pull requests