node.js - Sequelize nodejs 包含模型属性名称别名

标签 node.js sequelize.js

在我的 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/

相关文章:

node.js - 套接字.io : associate browser and computer with the connection

node.js - Node 安装有时有@但并不总是

node.js - JWT - 将它们存储在 LocalStorage 与 SessionStorage 中

mysql - 通过 GraphQL 获取数据

mysql - 与组中最大日期关联的 ID

postgresql - 使用UTC : false 时,Sequelize Date 列仍返回为UTC

node.js - 使用 Sequelize 一次更新两个关联表中的列

javascript - NodeJS : Convert table string (with\n\r and\t) to JSON

mysql - 根据时区时间选择数据

javascript - Sequelize beforeCreate 带有条件的钩子(Hook)