如何在sequelize中对嵌套的JSON字段进行排序

我正在尝试根据 sequence 字段订购 TabsTemplate 型号,但是订购无效 .

Routes

/* Get Manufacturer Tabs List */
router.get('/', function(req, res) {
  var manufacturer_id = req.params.manufacturer_id;
  models.Manufacturer.findAll({
    where: {
      id: manufacturer_id
    },
    include: [{
        model: models.TabsTemplate,
        order: [[models.sequelize.json("ManufacturerTab.sequence"), 'DESC']]
    }]
  }).  
  then(function(manufacturer_tabs) {  
      res.status(200).json(manufacturer_tabs);  
  }, function(error) {  
      res.status(500).send(error);  
  });  
});

Data

[
    {
        "id": 1,
        "manufacturer_name": "Brand",
        "manufacturer_logo_url": "/assets/img/brand-logo.jpg",
        "manufacturer_archived_status": false,
        "createdAt": "2018-12-06T15:06:01.703Z",
        "updatedAt": "2018-12-06T15:06:01.703Z",
        "TabsTemplates": [
            {
                "id": 1,
                "tab_name": "Tab 1",
                "createdAt": "2018-12-06T15:06:01.684Z",
                "updatedAt": "2018-12-06T15:06:01.684Z",
                "ManufacturerTab": {
                    "manufacturerId": 1,
                    "tabsTemplateId": 1,
                    "sequence": 2,
                    "createdAt": "2018-12-06T15:06:01.707Z",
                    "updatedAt": "2018-12-06T15:06:01.707Z"
                }
            },
            {
                "id": 2,
                "tab_name": "Tab 2",
                "createdAt": "2018-12-06T15:06:01.684Z",
                "updatedAt": "2018-12-06T15:06:01.684Z",
                "ManufacturerTab": {
                    "manufacturerId": 1,
                    "tabsTemplateId": 2,
                    "sequence": 1,
                    "createdAt": "2018-12-06T15:06:01.707Z",
                    "updatedAt": "2018-12-06T15:06:01.707Z"
                }
            },
            {
                "id": 3,
                "tab_name": "Tab 3",
                "createdAt": "2018-12-06T15:06:01.684Z",
                "updatedAt": "2018-12-06T15:06:01.684Z",
                "ManufacturerTab": {
                    "manufacturerId": 1,
                    "tabsTemplateId": 3,
                    "sequence": 3,
                    "createdAt": "2018-12-06T15:06:01.707Z",
                    "updatedAt": "2018-12-06T15:06:01.707Z"
                }
            },
            {
                "id": 4,
                "tab_name": "Tab 4",
                "createdAt": "2018-12-06T15:06:01.684Z",
                "updatedAt": "2018-12-06T15:06:01.684Z",
                "ManufacturerTab": {
                    "manufacturerId": 1,
                    "tabsTemplateId": 4,
                    "sequence": 4,
                    "createdAt": "2018-12-06T15:06:01.707Z",
                    "updatedAt": "2018-12-06T15:06:01.707Z"
                }
            }
        ]
    }
]

回答(1)

2 years ago

您可以使用 models.sequelize.col 代替 models.sequelize.json ,如:

[models.sequelize.col("TabsTemplates->ManufacturerTab.sequence"), 'DESC']