在我的 Controller 中,我有一个包含模型国家,它应该返回原始数据,因为 CON.name.CON 是包含模型的别名。我想更改列名称。 CON.name 到 countryName
代码
let include = [{
model: country , all:true }]
const Addr = await Address.findAll({
order: [['isDefaultAddress', 'DESC']],
where: {
Id: parseInt(params.req.body.id),
status: 1,
},
include:include,
raw:true,
attributes:{ exclude: [], include:['con.name']}
响应
CON.createdAt: "2020-04-02T12:35:52.205Z"
CON.id: 1
CON.name: "Afghanistan"
CON.updatedAt: "2020-04-02T12:35:52.205Z"
预期结果
countryCreatedAt: "2020-04-02T12:35:52.205Z"
countryId: 1
countryName: "Afghanistan"
countryUpdatedAt: "2020-04-02T12:35:52.205Z"
最佳答案
以这种方式使用 属性 :
attributes: [['con.id','countryId'],
['con.name', 'countryName'],
['con.createdAt','countryCreatedAt'],
['con.updatedAt','countryUpdatedAt']
]
关于node.js - Sequelize nodejs 包含模型属性名称别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61649744/