最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

仅订购嵌套模型数据序列

运维笔记admin15浏览0评论

仅订购嵌套模型数据序列

仅订购嵌套模型数据序列

我正在尝试仅基于整数列对嵌套的包含方案模型进行排序,但似乎代码无法正常工作。另外,数据从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
}));

这里,我只想对方案模型中的数据进行排序。样本回复:

"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": ".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": ".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": ".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

回答如下:

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中按moq排序...如果您只想按moq排序,则可以删除第一个字段。...>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论