yikart/AiToEarn · error · BadRequestException

参数错误: rating必须为like或unlike

Error message

参数错误: rating必须为like或unlike

What it means

After resolving the access token, rateVideo enforces an allow-list: rating must be exactly 'like' or 'unlike'. Any other value (wrong case, 'un_like', 'LIKED', numeric codes, etc.) throws this 400 BadRequestException. This is an enum-value validation error, not a missing-field error.

Source

Thrown at project/aitoearn-electron/server/src/modules/plat/tiktok/tiktok.controller.ts:369

      required: ['accountId', 'videoId', 'rating']
    }
  })
  async rateVideo(
    @GetToken() systemToken: TokenInfo,
    @Body('videoId') videoId: string,
    @Body('accountId') accountId: string,
    @Body('rating') rating: string
  ) {
    const userId = systemToken.id;
    if (!videoId || !userId || !accountId) {
      throw new BadRequestException('videoId, userId和accountId是必须的');
    }

    const accessToken = await this.tikTokAuthService.getUserAccessToken(accountId);

    // 在方法开始处验证
    if (!["like", "unlike"].includes(rating)) {
      throw new BadRequestException('参数错误: rating必须为like或unlike');
    }

    // 后续处理
    if (rating === "like") {
      return this.tikTokService.likeVideo(accessToken, userId, accountId, videoId);
    } else {
      return this.tikTokService.unlikeVideo(accessToken, userId, accountId, videoId);
    }
  }

  /**
   * 搜索TikTok视频
   */
  @Get('search')
  @ApiOperation({ summary: '搜索TikTok视频' })
  async searchVideos(
    @GetToken() systemToken: TokenInfo,
    @Query() filterDto: TikTokVideoFilterDto

View on GitHub (pinned to d3aa8bea5b)

Solutions

  1. Send the literal lowercase strings 'like' or 'unlike' as rating.
  2. Normalize/trim/lowercase the rating value on the client before sending.
  3. Constrain UI controls to a two-option toggle mapped to the allowed enum values.

Example fix

// before
await api.post('/tiktok/video/rate', { videoId, accountId, rating: 'Like' });
// after
const rating = action === 'like' ? 'like' : 'unlike';
await api.post('/tiktok/video/rate', { videoId, accountId, rating });
Defensive patterns

Strategy: validation

Validate before calling

const ALLOWED_RATINGS = ['like', 'unlike'] as const;
if (!ALLOWED_RATINGS.includes(rating as any)) {
  throw new Error(`rating must be like or unlike, got: ${rating}`);
}

Type guard

type Rating = 'like' | 'unlike';
function isRating(v: unknown): v is Rating {
  return v === 'like' || v === 'unlike';
}

Try / catch

try {
  await api.post('/tiktok/video/rate', { videoId, accountId, rating });
} catch (e) {
  if (e.response?.status === 400 && String(e.response.data?.message).includes('rating')) {
    console.warn('Invalid rating value, expected like|unlike');
  }
}

Prevention

When it happens

Trigger: Posting rating values such as 'Like', 'dislike', 'like ', 1, or undefined to the rate-video endpoint.

Common situations: UI sending human-readable labels instead of enum values; case-sensitivity bugs; localized strings passed as rating.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of yikart/AiToEarn@d3aa8bea5b (2026-08-31). Data as JSON: /api/errors/379c89d43b87ae96. Report an issue: GitHub.