json TypeORM find方法返回无意义数据

owfi6suc  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(136)

因此,我尝试创建一个简单的GET路由来从DB获取一些命令。
我的订单实体如下所示:

@Entity('orders')
export class Order extends BaseEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @CreateDateColumn()
    createdAt: string;

    @Column()
    price: number;

    @ManyToOne(() => Status, (status) => status.order)
    status: Status;

    @OneToMany(
        () => OrderHaveProducts,
        (OrderHaveProducts) => OrderHaveProducts.product
    )
    products: OrderHaveProducts[];
}

我尝试获取每个订单,订购的产品,它们存储在另一个表中,我使用了一个名为OrderHaveProducts的实体,它看起来像这样:

@Entity('orders_have_products')
export class OrderHaveProducts extends BaseEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @ManyToOne(() => Order, (order) => order.products)
    @JoinColumn({ name: 'order_id' })
    order: Order;

    @ManyToOne(() => Product, (product) => product.orders)
    @JoinColumn({ name: 'product_id' })
    product: Product;

    @ManyToMany(() => Ingredient)
    @JoinTable({
        name: 'ordered_products_have_removed_ingredients',
        joinColumn: {
            name: 'ordered_product_id',
            referencedColumnName: 'id',
        },
        inverseJoinColumn: {
            name: 'ingredient_id',
            referencedColumnName: 'id',
        },
    })
    removed_ingredients: Ingredient[];

    @ManyToMany(() => Ingredient)
    @JoinTable({
        name: 'custom_products_have_ingredients',
        joinColumn: {
            name: 'ordered_product_id',
            referencedColumnName: 'id',
        },
        inverseJoinColumn: {
            name: 'ingredient_id',
            referencedColumnName: 'id',
        },
    })
    added_ingredients: Ingredient[];
}

在我的数据库中,在我的OrderHaveProducts表中,我有以下数据:OrderHaveProducts table in PHPMyAdmin
其中product_id 4是名为“BBQ”的比萨饼,product_id 2是名为“Margarita”的比萨饼。
我遇到的问题是,当我执行这段代码时:

const orders: Order[] = await Order.find({
    relations: {
        status: true,
        products: {
            product: true,
            removed_ingredients: true,
            added_ingredients: true,
        },
    },
});

返回的JSON如下所示:

{
    "status": 200,
    "data": {
        "orders": [
            {
                "id": 1,
                "createdAt": "2023-02-11T22:50:18.721Z",
                "price": 27,
                "status": {
                "id": 4,
                "name": "finished"
                },
                "products": []
            },
            {
                "id": 2,
                "createdAt": "2023-02-11T22:50:18.735Z",
                "price": 15,
                "status": {
                    "id": 4,
                    "name": "finished"
                },
                "products": [
                    {
                        "id": 1,
                        "product": {
                            "id": 2,
                            "name": "Margarita",
                            "price": 12,
                            "picture: "https://medias.delarte.fr/media/sys_master/images/hdb/h49/8875895488542.png"
                        },
                        "removed_ingredients": [
                            {
                                "id": 5,
                                "name": "mozzarella",
                                "stock": 50
                            }
                        ],
                        "added_ingredients": []
                    }
                ]
            }
        ]
    }
}

我们可以清楚地看到,我的第一个订单没有任何产品(当在DB中,我们看到它有2),第二个订单有1个产品(如预期),但它不是好产品。
我尝试只从OrderHaveProducts表中获取数据,并且成功地获取了我在DB中看到的数据:

{
    "status": 200,
    "data": {
        "orders": [
            {
                "id": 1,
                "order": {
                    "id": 1,
                    "createdAt": "2023-02-11T22:50:18.721Z",
                    "price": 27
                },
                "product": {
                    "id": 2,
                    "name": "Margarita",
                    "price": 12,
                    "picture": "https://medias.delarte.fr/media/sys_master/images/hdb/h49/8875895488542.png"
                }
            },
            {
                "id": 2,
                "order": {
                    "id": 1,
                    "createdAt": "2023-02-11T22:50:18.721Z",
                    "price": 27
                },
                "product": {
                    "id": 4,
                    "name": "BBQ",
                    "price": 15,
                    "picture": "https://cdn.shopify.com/s/files/1/0508/2179/1903/articles/25-Comment_cuire_pizza_barbecue_1500x.jpg?v=1619600472"
                }
            },
            {
                "id": 3,
                "order": {
                    "id": 2,
                    "createdAt": "2023-02-11T22:50:18.735Z",
                    "price": 15
                },
                "product": {
                    "id": 4,
                    "name": "BBQ",
                    "price": 15,
                    "picture": "https://cdn.shopify.com/s/files/1/0508/2179/1903/articles/25-Comment_cuire_pizza_barbecue_1500x.jpg?v=1619600472"
                }
            }
        ]
    }
}

如果有人能告诉我这一切是怎么回事,那将是难以置信的,谢谢!

xesrikrc

xesrikrc1#

我终于找到了问题(问题?)。我实际上错误地设置了我的订单实体和OrderHasProducts之间的关系。
所以在我的订单实体中,我的关系是这样的:

@OneToMany(
    () => OrderHaveProducts,
    (OrderHaveProducts) => OrderHaveProducts.product // <-- Here I linked the entity to the product, when I should have linked it to order
)
products: OrderHaveProducts[];

因此,好的关系应该是这样的:

@OneToMany(
    () => OrderHaveProducts,
    (OrderHaveProducts) => OrderHaveProducts.order
)
products: OrderHaveProducts[];

第二个问题来自于OrderHaveProducts实体,另一次,我错过了链接实体,所以我得到了这个:

@ManyToOne(() => Order, (order) => order.products)
@JoinColumn({ name: 'order_id' })
order: Order;

@ManyToOne(() => Product, (product) => product.orders)
@JoinColumn({ name: 'product_id' })
product: Product;

当良好的关系应该是这样的:

@Column()
order_id: number;

@Column()
product_id: number;

@ManyToOne(() => Order, (order) => order.id)
@JoinColumn({ name: 'order_id' })
order: Order;

@ManyToOne(() => Product, (product) => product.id)
@JoinColumn({ name: 'product_id' })
product: Product;

所以现在我成功地得到了好的数据。
希望它能在未来帮助任何人;)

相关问题