Not able to update my tables using Typeorm?

Viewed 210

I am trying to make a simple reddit clone. I have a Space enitity which is like a community in actual Reddit.

@ObjectType()
@Entity()
export class Spaces extends BaseEntity {
  @Field()
  @PrimaryGeneratedColumn("uuid")
  spaceId: string;

  @Field()
  @Column("text")
  adminId: string;

  @Field()
  @Column("text", { default: "public" })
  type: string;

  @Field()
  @Column("text", { unique: true })
  spaceName!: string;

  @Field()
  @OneToMany(() => Post, (post) => post.space)
  posts: Post;


  @Field()
  @Column("text", { array: true , default:[]} )
  bannedUserIds: string[];


  @ManyToMany(() => User , (user) => user.spaces , {
    cascade: true
  })
  @JoinTable()
  users: User[]
}

And this is my User Entity

@ObjectType()
@Entity()
export class User extends BaseEntity {
  @Field(() => ID)
  @PrimaryGeneratedColumn("uuid")
  id!: string;

  @Field()
  @Column("text" , {unique: true})
  studentId!: string;

  @Field()
  @Column("text")
  name!: string;

  @Field()
  @Column("text", { unique: true })
  username!: string;

  @Field()
  @Column("text", { unique: true })
  email!: string;

  @Field()
  @Column("text")
  gender!: string;

  @Field()
  @Column("bool", { default: false })
  godAdmin!: boolean;

  @Field()
  @Column("bool", { default: true })
  isConfirmed!: boolean;

  @Field()
  @Column("text")
  password!: string;

  @Field()
  @Column({
    type: "text",
    default:
      "https://image.shutterstock.com/image-vector/blank-avatar-placeholder-on-transparent-600w-1097191784.jpg",
  })
  avatarUrl!: string;

  @Field()
  @Column("bool", { default: false })
  isBanned: boolean;

  @Field()
  @Column("int", { default: 0 })
  striked: number;

  @Field()
  @Column({ type: "text", nullable: true, default: "" })
  bio: string;

  @Field()
  @Column({ type: "text", nullable: true, default: "" })
  instagramAcc!: string;

  @Field()
  @Column({ type: "text", nullable: true, default: "" })
  twitterAcc!: string;

  @Field()
  @Column({ type: "text", nullable: true, default: "" })
  GithubAcc!: string;

  @OneToMany(() => Updoot, (updoot) => updoot.user)
  updoots: Updoot[];

  @Field()
  @OneToMany(() => Post, (post) => post.creator)
  posts: Post[];
  
  
  @ManyToMany(() => Spaces , (space) => space.users )
  spaces: Spaces[]
}

I am also using type-graphql to make my APIs. I have a createSpace mutation


 @Mutation(() => Boolean)
  @UseMiddleware(isAuth)
  async createSpace(
    @Arg("spaceName") spaceName: string,
    @Ctx() { req }: MyContext
  ): Promise<boolean> {
    const user = await User.findOne({id: req.session.userId});
    await Spaces.create({
      spaceName: spaceName,
      adminId: req.session.userId,
      users: [user]
    }).save();

    return true;
  }

But this is giving me an error "No Overload Matches This Call". I want to add Admin of this space when this space is created. Can anyone help me to solve this issue?

enter image description here

1 Answers

Quite hard to know the exact problem without reading all the codes.

From what I see, you are using the entity directly where repository should be used in practice. Can you try to modify the code to adopt repository pattern? See if it helps you to get rid of the error. If not, you may better share the code.

import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

class xxxx {
    constructor(
       @InjectRepository(User)
       private usersRepository: Repository<User>,
       @InjectRepository(Spaces)
       private spacesRepository: Repository<Spaces>,
    ) {}
 
    @Mutation(() => Boolean)
    @UseMiddleware(isAuth)
    async createSpace(
       @Args('spaceName') spaceName: string, 
       @Ctx() { req }: MyContext) {
       const user = this.usersRepository.findOne({ id: req.session.userId });
       await this.spacesRepository.create({ spaceName, adminId: req.session.userId, users: [user]}).save();
       return true;
    }

}
Related