大家好,
我使用Nodejs
和Koajs
用Typeorm
制作了一个API。
当我调用我的API端点之一时,我遇到了一个小问题。端点应该创建新的内容。而是在尝试保存时抛出错误。
错误:
{
"success": false,
"error": {
"message": "ER_WRONG_VALUE_COUNT_ON_ROW: Column count doesn't match value count at row 1",
"code": "ER_WRONG_VALUE_COUNT_ON_ROW",
"errno": 1136,
"sqlMessage": "Column count doesn't match value count at row 1",
"sqlState": "21S01",
"index": 0,
"sql": "INSERT INTO `content`(`id`, `title`, `content`) VALUES ('9c29e73c-9bd0-4cf3-bea4-ad38f5082790', `title` = 'Test', `content` = 'Contenthaha', DEFAULT)",
"name": "QueryFailedError",
"query": "INSERT INTO `content`(`id`, `title`, `content`) VALUES (?, ?, DEFAULT)",
"parameters": [
"9c29e73c-9bd0-4cf3-bea4-ad38f5082790",
{
"title": "Test",
"content": "Contenthaha"
}
]
}
}
端点:
@Post('/content')
async postOne(@Body() title: string, content: string) {
const { repository } = this;
try {
const newContent = repository.create({
title,
content
});
await repository.save(newContent);
} catch (exception) {
return { 'success': false, 'error': exception };
}
return { 'success': true, 'message': 'Content was successfully created' };
}
当我在引发错误之前在控制器中返回newContent
时,我得到了在Postman
中返回的这个奇怪的JSON:
{
"title": {
"title": "Test",
"content": "Contenthaha"
}
}
我的模型:
import {
Entity,
PrimaryGeneratedColumn,
Column,
} from 'typeorm';
@Entity()
export class Content {
@PrimaryGeneratedColumn('uuid')
id!: number;
@Column()
title!: string;
@Column({ type: 'longtext' })
content!: string;
}
希望我能帮助别人,因为我不在这里。
谢谢!