How in nest js made follow, unfollow functionality

Viewed 42

I have User.entity.ts


@Table({ tableName: 'users' })
export class User extends Model<User, userCreationAttrs> {
  @Column({ type: DataType.INTEGER, unique: true, autoIncrement: true, primaryKey: true })
  id: number

  @Column({ type: DataType.ARRAY(DataType.JSONB), defaultValue: [] })
  friends: User[]
}

and have users.controller.ts


@Controller('users')
export class UsersController {

  constructor(private usersService: UsersService) { }

  @Patch('friends/:friendId')
  toggleFriend(@Param('friendId') friendId: string, @Body() currentUserId: string) {

    return this.usersService.toggleFriend(+currentUserId, +friendId)
  }
}

and users.service.ts


@Injectable()
export class UsersService {

  constructor(@InjectModel(User) private userRepository: typeof User) { }

  async toggleFriend(userId: number, friendId: number,) {
    const user = await this.userRepository.findOne({ where: { id: userId } })
    const friend = await this.userRepository.findOne({ where: { id: friendId } })
    if (user.friends.includes(friendId)) { //and i have typescript err this An argument of type "number" cannot be assigned to a parameter of type "User".
      user.friends = user.friends.filter(id => id !== friendId) //and this
      friend.friends = friend.friends.filter(id => id !== friendId) //and this
    } else {
      user.friends = [...user.friends, friend]
      friend.friends = [...friend.friends, user]
    }

    await user.save()
    await friend.save()

    return true
  }
}

When i run the script npm run start:dev i get an error

Executing (default): SELECT "id", "name", "email", "password", "birthDate", "city", "gender", "avatarPath", "banned", "banReason", "friends", "createdAt", "updatedAt" FROM "users" AS "User" WHERE "User"."id" = NaN;
[Nest] 8492  - 23.07.2022, 18:43:28   ERROR [ExceptionsHandler] столбец "nan" не существует

I can't figure out what's the matter. Help me please. And I can't figure out how to do it better follow, unfollow functionality.

0 Answers
Related