我试图仅基于整数列对嵌套的包含方案模型进行排序,但似乎代码无法正常工作。另外,数据是从MYSQL
底部向上获取的,并显示为输出。
示例代码:
[err, products] = await to(
Product_Inventory_Mapping.findAll({
include: [
{
model: Product,
as: "product_details",
where: filterClause,
include: [
{
model: Unit,
as: "unit_details"
},
{
model: Brand,
as: "brand_details"
},
{
model: Category,
as: "category_details"
},
{
model: Sub_Brand,
as: "sub_brand_details"
},
{
model: Sub_Category,
as: "sub_category_details"
},
{
model: Brand_Company,
as: "brand_company_details"
},
{
model: Scheme,
as: "schemes",
attributes: [`moq`, 'discount', `description`],
order: [[{ model: Scheme, as: "schemes" }, "moq", "ASC"]],
where: {
status: 1
},
required: false
}
]
}
],
order: [
[{ model: Product, as: "product_details" }, "mrp", "ASC"]
],
where: {
user_id: aligned_distributors,
brand_company_id: filtered_brand_company,
count: {
[Op.gt]: 0
},
sp: {
[Op.gt]: 0
}
},
attributes: [
`product_id`,
"user_id",
"count",
"sp",
"occ",
"brand_company_id"
],
limit: limit,
offset: offset,
subQuery: false
}));
在这里,我只想对Scheme Model中的数据进行排序。 样本回复:
"products": [
{
"product_id": 115,
"user_id": 1003,
"count": 50,
"sp": 9.09,
"occ": 9.09,
"brand_company_id": 11,
"product_details": {
"id": 115,
"category_id": 2,
"sub_category_id": 12,
"brand_company_id": 11,
"brand_id": 24,
"sub_brand_id": 44,
"sku_code": null,
"min_order_qty": "27",
"description": "Nandini Butter Milk Tetra Pack, 200 Ml",
"unit_id": 4,
"weight": "200ml",
"mrp": "10.00",
"barcode": "",
"image_url": "https://xxxxxx.s3.ap-south-1.amazonaws.com/products/1565919229Unknown-min-5.jpg",
"created_at": "2019-08-12T13:44:01.000Z",
"updated_at": "2019-08-15T20:03:49.000Z",
"unit_details": {
"id": 4,
"name": "Pieces",
"created_at": null,
"updated_at": null
},
"brand_details": {
"id": 24,
"name": "Nandini Butter Milk",
"brand_company_id": 11,
"created_at": null,
"updated_at": null
},
"category_details": {
"id": 2,
"name": "Packaged Food",
"image_url": "https://zzzz.s3.ap-south-1.amazonaws.com/images/category/food-min.png",
"created_at": "2019-08-11T11:36:52.000Z",
"updated_at": "2019-08-11T11:36:52.000Z"
},
"sub_brand_details": {
"id": 44,
"name": "Nandini Butter Milk",
"brand_id": 24,
"created_at": null,
"updated_at": null
},
"sub_category_details": {
"id": 12,
"name": "Dairy products (Butter, Cheese etc)",
"category_id": 2,
"created_at": null,
"updated_at": null
},
"brand_company_details": {
"id": 11,
"name": "Karnataka Co-operative Milk Producers",
"image_url": "https://xxxxx.s3.ap-south-1.amazonaws.com/images/company/kmf_logo.jpg",
"created_at": "2019-08-12T13:10:18.000Z",
"updated_at": "2019-08-12T13:10:18.000Z"
},
"schemes": [
{
"moq": 30,
"discount": 1.5,
"description": "8 % offer"
},
{
"moq": 20,
"discount": 1,
"description": "20 % offer"
},
{
"moq": 40,
"discount": 2,
"description": "40 % offer"
}
]
}
}]
方案通过product_id属于产品。我希望Scheme数组按moq ASC
排序,以便该值按最小起订量的顺序排列:20 > 30 > 40
。
答案 0 :(得分:0)
AFAIK,所包含的模型中的order
子句不起作用。但是,您可以引用主模型中包含的模型。我认为如果将两个order
子句合并为一个子句,这应该可以工作:
Product_Inventory_Mapping.findAll({
include: [
..... lots of includes here ....
],
order: [
[{ model: Product, as: "product_details" }, "mrp", "ASC"],
[{ model: Scheme, as: "schemes" }, "moq", "ASC"]]
],
这将在mrp中按最小起订量排序...如果您只想按最小起订量排序,则可以删除第一个字段。