最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Sequelize nodejs包含的模型属性名称别名

网站源码admin14浏览0评论

Sequelize nodejs包含的模型属性名称别名

Sequelize nodejs包含的模型属性名称别名

在我的控制器中,我有一个包含模型的国家,应该将原始数据返回为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']
              ] 
发布评论

评论列表(0)

  1. 暂无评论