我有2个模型Manufacturer
和TabsTemplate
,具有多对多关联。 ManufacturerTab
模型具有sequence
列。当我尝试使用TabsTemplate
列对sequence
进行排序时,出现500错误。
关联模型:
Manufacturer.belongsToMany(models.TabsTemplate, { through: ManufacturerTab, foreignKey: 'manufacturerId'});
TabsTemplate.belongsToMany(models.Manufacturer, { through: ManufacturerTab, foreignKey: 'tabsTemplateId', as: 'tabstemp'});
路线:
/* Get Manufacturer Tabs List */
router.get('/', function(req, res) {
var manufacturer_id = req.params.manufacturer_id;
models.Manufacturer.findAll({
where: {
id: manufacturer_id
},
order: [[models.TabsTemplate, 'ManufacturerTab.sequence', 'DESC']],
include: [{
model: models.TabsTemplate
}]
}).
then(function(manufacturer_tabs) {
res.status(200).json(manufacturer_tabs);
}, function(error) {
res.status(500).send(error);
});
});
数据:
[
{
"id": 1,
"manufacturer_name": "Brand 1",
"manufacturer_logo_url": "/assets/img/brand-logo.jpg",
"manufacturer_archived_status": false,
"createdAt": "2018-12-05T11:00:22.282Z",
"updatedAt": "2018-12-05T11:00:22.282Z",
"TabsTemplates": [
{
"id": 1,
"tab_name": "Tab 1",
"createdAt": "2018-12-05T11:00:22.264Z",
"updatedAt": "2018-12-05T11:00:22.264Z",
"ManufacturerTab": {
"manufacturerId": 1,
"tabsTemplateId": 1,
"sequence": 2,
"createdAt": "2018-12-05T11:00:22.286Z",
"updatedAt": "2018-12-05T13:09:20.179Z"
}
},
{
"id": 2,
"tab_name": "Tab 2",
"createdAt": "2018-12-05T11:00:22.264Z",
"updatedAt": "2018-12-05T11:00:22.264Z",
"ManufacturerTab": {
"manufacturerId": 1,
"tabsTemplateId": 2,
"sequence": 1,
"createdAt": "2018-12-05T11:00:22.287Z",
"updatedAt": "2018-12-05T13:09:20.175Z"
}
},
{
"id": 3,
"tab_name": "Tab 3",
"createdAt": "2018-12-05T11:00:22.264Z",
"updatedAt": "2018-12-05T11:00:22.264Z",
"ManufacturerTab": {
"manufacturerId": 1,
"tabsTemplateId": 3,
"sequence": 3,
"createdAt": "2018-12-05T11:00:22.287Z",
"updatedAt": "2018-12-05T13:09:20.181Z"
}
}
]
}
]