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: TikTokVideoFilterDtoView on GitHub (pinned to d3aa8bea5b)
Solutions
- Send the literal lowercase strings 'like' or 'unlike' as rating.
- Normalize/trim/lowercase the rating value on the client before sending.
- 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
- Define a Rating union type and derive UI options from it.
- Normalize user input (trim + lowercase) before mapping to the enum.
- Never pass raw localized labels as rating values.
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
- userId是必需的
- token和mail是必须的
- accountId, videoId和commentId是必须的
- videoId, userId和accountId是必须的
- accountId和视频大小是必须的
AI-assisted analysis of yikart/AiToEarn@d3aa8bea5b (2026-08-31).
Data as JSON: /api/errors/379c89d43b87ae96.
Report an issue: GitHub.