【问题标题】:Sequelize,findAll, attributes can not rename columnSequelize,findAll,属性不能重命名列
【发布时间】:2021-06-05 02:24:45
【问题描述】:

我的表格数据:

Mytable(id,col1,col2,time)

有数据

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.js sequelize.js


    【解决方案1】:
    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'] -> 内第一个 {}

    【讨论】:

    • code: 'ER_BAD_FIELD_ERROR', errno: 1054, sqlState: '42S22', sqlMessage: "'field list'中的未知列'newnamecol1'", sql: "SELECT col1, id AS newnameid, newnamecol1 FROM mytable AS mytable WHERE mytable.id = 'othervalue';", 参数:未定义
    【解决方案2】:

    使用 findAll 和

          const convertedData = await rows.map(arrObj => {
          return {
            newname: reqbody.olddata
          }})
    

    【讨论】:

      猜你喜欢
      • 2023-01-02
      • 2017-02-11
      • 2020-04-08
      • 2017-04-24
      • 2020-05-13
      • 2019-11-17
      • 2021-10-02
      • 2020-03-15
      • 2019-12-31
      相关资源
      最近更新 更多