【发布时间】:2020-08-15 08:50:53
【问题描述】:
我正在尝试开发一个小型应用程序来记录烹饪食谱。为此,我用 nestJS 声明了 2 个实体,允许我管理食谱和另一个来管理成分。我还创建了第三个实体来记录所需成分的数量:
// recipe.entity.js
@Entity()
export class Recipe {
@PrimaryGeneratedColumn()
id: number
@Column('datetime')
createdAt: Date
@Column('datetime')
updatedAt: Date
@Column('varchar', { length: 100 })
title: string;
@Column('varchar', {nullable: true})
image: string;
@OneToMany(type => RecipeIngredients, recipeIngredients => recipeIngredients.recipe)
ingredients: RecipeIngredients[];
}
// ingredient.entity.js
@Entity()
export class Ingredient {
@PrimaryGeneratedColumn()
id: number
@Column('datetime')
createdAt: Date
@Column('datetime')
updatedAt: Date
@Column('varchar', { length: 100 })
name: string;
@Column('varchar', {nullable: true})
image: string;
@OneToMany(type => RecipeIngredients, recipeIngredients => recipeIngredients.ingredient)
recipes: RecipeIngredients[];
}
// recipe_ingredients.entity.js
@Entity()
export class RecipeIngredients {
@PrimaryGeneratedColumn()
id: number
@ManyToOne(type => Recipe, recipe => recipe.ingredients)
recipe: Recipe
@ManyToOne(type => Ingredient)
ingredient: Ingredient
@Column()
quantity: string;
}
首先,我希望能够检索包含必要成分列表的食谱:
const recipe = await this.recipesRepository.createQueryBuilder('recipe')
.where('recipe.id = :recipeId', {recipeId: _id})
.leftJoin('recipe.ingredients', 'recipe_ingredients')
.leftJoin('recipe_ingredients.ingredient', 'ingredient')
.getMany();
但是这个方法只返回我的食谱对象,没有成分...
[
{
"id": 1,
"createdAt": "2020-04-30T09:12:22.000Z",
"updatedAt": "2020-04-30T09:12:22.000Z",
"title": "Test",
"image": null
}
]
从那里,我迷路了...如何直接从我的服务中获取我的成分列表(至少是名称和数量字段)?
提前感谢您的帮助。
【问题讨论】:
标签: mysql node.js typescript nestjs