Метаданные объекта для роли User # не найдены при использовании псевдонимов Paths - PullRequest
0 голосов
/ 14 марта 2020

в настоящее время User.ts и Role.ts находится в той же папке

, когда я пытался импортировать Role.ts в User.ts , он не работает с псевдонимами пути, например:

- это сломал код

import { Role } from "@entity/Role";

- это нормально

import { Role } from "./Role";

ниже находятся файлы

User.ts

import { Entity, Column, Unique, ManyToOne } from "typeorm";

import { Length, IsNotEmpty, IsEmail } from "class-validator";
import * as bcrypt from "bcryptjs";

import { General } from "@util/EntityGeneral";
import { Role } from "@entity/Role";

@Entity()
@Unique(["userName", "email"])
export class User extends General {
  @Column()
  @Length(4, 36)
  userName: string;

  @Column()
  @Length(4, 36)
  firstName: string;

  @Column()
  @Length(4, 36)
  lastName: string;

  @Column()
  @IsEmail()
  email: string;

  @Column()
  @Length(4, 36)
  password: string;

  @Column()
  @Length(4, 128)
  website: string;

  @Column()
  @Length(4, 36)
  phone: string;

  @Column()
  @Length(4, 128)
  twitter: string;

  @Column()
  @Length(4, 128)
  facebook: string;

  @Column()
  @Length(0, 300)
  bio: string;

  @Column()
  profileImage: string;

  @ManyToOne(
    type => Role,
    role => role.users
  )
  role!: Role;

  constructor(props: {
    username: string;
    first_name: string;
    last_name: string;
    email: string;
    password: string;
    website?: string;
    phone?: string;
    twitter?: string;
    facebook?: string;
    bio?: string;
    profile_image?: string;
    role?: Role;
  }) {
    super();
    var {
      username,
      first_name,
      last_name,
      email,
      password,
      website,
      phone,
      twitter,
      facebook,
      bio,
      profile_image,
      role
    } = props;
    this.userName = username;
    this.firstName = first_name;
    this.lastName = last_name;
    this.email = email;
    this.password = password;
    this.website = website;
    this.phone = phone;
    this.twitter = twitter;
    this.facebook = facebook;
    this.bio = bio;
    this.profileImage = profile_image;
    this.role = role;
  }

  hashPassword() {
    this.password = bcrypt.hashSync(this.password, 8);
  }

  checkIfUnencryptedPasswordIsValid(unencryptedPassword: string) {
    return bcrypt.compareSync(unencryptedPassword, this.password);
  }
}

role.ts

import {
  Entity,
  ManyToMany,
  OneToMany,
  JoinTable,
  Column,
  Unique
} from "typeorm";
import { Length, IsNotEmpty } from "class-validator";

import { General } from "@util/EntityGeneral";
import { User } from "@entity/User";
import { Permission } from "@entity/Permission";

@Entity()
@Unique(["name"])
export class Role extends General {
  @Column()
  name: string;

  @Column()
  @Length(4, 128)
  description: string;

  @ManyToMany(type => Permission)
  @JoinTable()
  permissions: Permission[];

  @OneToMany(
    type => User,
    user => user.role
  )
  users: User[];

  constructor(props: {
    name: string;
    description: string;
    permissions?: any;
    users?: any;
  }) {
    super();
    var { name, description, permissions, users } = props;
    this.name = name;
    this.description = description;
    this.permissions = permissions;
    this.users = users;
  }
}

это, кажется, происходит только с классами typeORM, другие классы и функции работают нормально.

...