我可以将字段作为Sequelize js上的嵌套对象吗?

我在sequelize.js中有以下查询:

getBookingById: (req, res) => { models.booking.findAll({ where: { id: { $in: [1, 2, 3] }, }, include: [{ model: models.cust, attributes: ['name', 'id'], }], attributes: [['id', 'bookingId'], 'propertyKey', 'propertyId'] }) .then((result) => { if (!result) return res.status(204); return res.status(200).json(result); }).catch(err => res.status(500); }; 

我的回答如下:

 [{ "bookingId": 1, "propertyKey": "ABC", "propertyId": "123", "cust": { "name": "David David", "id": 8 } }, { "bookingId": 2, "propertyKey": "ABC", "propertyId": "123", "cust": { "name": "David David", "id": 8 } }] 

我想返回作为嵌套对象的propertyKeypropertyId字段为:

 "property": { "propertyKey": "ABC", "propertyId": "123", } 

是否有可能在续集查询中做到这一点,或者,我应该parsing后的结果?

您可以在返回之前更改resultvariables。

  .then((result) => { if (!result) return res.status(204); for (var i = 0; i < result.length ; i++){ result[i].property = { "propertyKey" : result.propertyKey , "propertyId": result.propertyId } delete result[i].propertyKey; delete result[i].propertyId; } return res.status(200).json(result); }).catch(err => res.status(500);