Hi everyone,
我用 Nodejs
使用 Koajs
和 Typeorm
创建了一个API .
当我调用我的一个API endpoints 时,我遇到了一个小问题 . endpoints 应该创建新内容 . 相反,它在尝试保存时会抛出错误 .
The error:
{
"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"
}
]
}
}
The endpoint:
@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"
}
}
My model:
import {
Entity,
PrimaryGeneratedColumn,
Column,
} from 'typeorm';
@Entity()
export class Content {
@PrimaryGeneratedColumn('uuid')
id!: number;
@Column()
title!: string;
@Column({ type: 'longtext' })
content!: string;
}
我希望有人可以提供帮助,因为我已经失去了大脑 .
提前致谢!