首页 > 解决方案 > Sequelize,findAll,属性不能重命名列

问题描述

我的表数据:

Mytable(id,col1,col2,时间)

有数据

Mytable("1","val1","val2","time")

我通过这段代码获取数据:

async function getData() {
   const mytables = await Mytable.findAll(
     { where: { id: othervalue }},
     { attributes: ['col1',['id', 'newnameid'],'newnamecol1']}
   );
   console.log(JSON.stringifty(mytables));
}
getData();

我只想要这样的数据:

[ {'newnameid':'1','newnamecol1':'val1'} ]

但我得到了这个:

[ {'id':'1','col1':'val1','col2':'val2,'time':'time''} ]

嗯,这段代码可能有什么问题?

标签: node.jssequelize.js

解决方案


async function getData() {
   const mytables = await Mytable.findAll(
     { 
         where: { id: othervalue },
         attributes: ['col1',['id', 'newnameid'],'newnamecol1']
     },

   );
   console.log(JSON.stringifty(mytables));
}
getData();

更新:
attributes: ['col1',['id', 'newnameid'],'newnamecol1'] -> 内部优先{}


推荐阅读