创纪录的韩元';t使用Nestjs和TypeOrm保存到数据库(查询工作)

创纪录的韩元';t使用Nestjs和TypeOrm保存到数据库(查询工作),nestjs,typeorm,Nestjs,Typeorm,当我发布请求时,它不会将我的实体保存到数据库中,也不会报告任何错误或警告 控制器: @Controller('recipes') export class RecipesController { constructor(private readonly recipeService: RecipesService) {} @Get() async findAll(): Promise<Recipe[]> { return this.recipeService.fi

当我发布请求时,它不会将我的实体保存到数据库中,也不会报告任何错误或警告

控制器:

@Controller('recipes')

export class RecipesController {
  constructor(private readonly recipeService: RecipesService) {}
  @Get()
  async findAll(): Promise<Recipe[]> {
    return this.recipeService.findAll();
  }

  @Post()
  async create(@Body() createRecipeDto: Recipe) {
    this.recipeService.create(createRecipeDto);
  }
}
我使用邮递员的请求:

curl -X POST \
  http://localhost:3000/recipes \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: f23b5d42-1c40-4dae-b9b8-b32b733f38b4' \
  -H 'cache-control: no-cache' \
  -d '{
    "name":"recipe5",
    "description": "desc",
    "image": "http://..",
    "ingredients": ["-"],
    "instructions": ["1"],
    "prepTime": "1:20",
    "cookTime": "1:00",
    "yield": "8 servings",
    "rating": 4,
    "keywords": ["1"],
    "categories": ["cat1", "cat2"],
    "cuisine": "American",
    "draft": false
}'
它返回201。 我的GET调用只返回我手动创建的条目。

我发现了问题

TypeOrm实际上并没有将实体保存到存储器中。为此,您必须调用
存储库。保存

我的服务中的此更改修复了我的问题:

发件人:

致:

我发现了问题

TypeOrm实际上并没有将实体保存到存储器中。为此,您必须调用
存储库。保存

我的服务中的此更改修复了我的问题:

发件人:

致:

@Entity()
export class Recipe {
  @PrimaryGeneratedColumn()
  id: number;

  //   @IsString()
  @Column()
  name: string;

  //   @IsString()
  @Column('text')
  description?: string;

  //   @IsString()
  @Column()
  image: string;

  //   @IsArray()
  ingredients: string[];

  //   @IsArray()
  instructions: string[];

  //   @IsString()
  @Column()
  prepTime?: string;

  //   @IsString()
  @Column()
  cookTime?: string;

  //   @IsString()
  @Column()
  yield?: string;

  //   @IsNumber()
  @Column('int')
  rating?: number;

  //   @IsArray()
  keywords?: string[];

  //   @IsArray()
  categories: string[];

  //   @IsString()
  @Column()
  cuisine?: string;

  //   @IsBoolean()
  @Column('boolean')
  draft?: boolean;
}
curl -X POST \
  http://localhost:3000/recipes \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: f23b5d42-1c40-4dae-b9b8-b32b733f38b4' \
  -H 'cache-control: no-cache' \
  -d '{
    "name":"recipe5",
    "description": "desc",
    "image": "http://..",
    "ingredients": ["-"],
    "instructions": ["1"],
    "prepTime": "1:20",
    "cookTime": "1:00",
    "yield": "8 servings",
    "rating": 4,
    "keywords": ["1"],
    "categories": ["cat1", "cat2"],
    "cuisine": "American",
    "draft": false
}'
const d = await this.recipeRepository.create(recipe);
const d = await this.recipeRepository.save(recipe);