gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:19:58 46 4
gpt4 key购买 nike

我正在尝试根据 TabsTemplate 字段订购 sequence 模型,但是,订购不起作用。

路由

/* 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);
});
});

数据
[
{
"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"
}
}
]
}
]

最佳答案

您可以使用 models.sequelize.col 而不是 models.sequelize.json ,例如:
[models.sequelize.col("TabsTemplates->ManufacturerTab.sequence"), 'DESC']

关于sequelize.js - 如何在 sequelize 中对嵌套的 JSON 字段进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53704934/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com