NodeJS 如何在sequelize中使用`findAll()`而不使用`raw:true`获取数据值?

mec1mxoz  于 2023-05-28  发布在  Node.js
关注(0)|答案(2)|浏览(217)

我希望在sequelize中使用findAll()而不使用raw:true时获取datavalues

Activities.findAll({
         include: [
         {       
           all: true         
          }   
         ],  
       })
        .then(activities => {   
        });
crcmnpdw

crcmnpdw1#

如果没有raw:true,你必须循环遍历每个对象并从中获取值,如下所示:

Activities.findAll({
    include: [{
        all: true
    }],
}).then(activities => {
    return activities.map( activity => el.get({ plain: true }) );
});

raw : true,在使用include时生成.名称,这个问题可以通过nest : true解决

Activities.findAll({
    raw : true ,
    nest: true , // <--- The issue of raw true, will be solved by this
    include: [{
        all: true
    }],
}).then(activities => {
    console.log(activities);
});
4ktjp1zp

4ktjp1zp2#

这是我在不使用row: true的情况下求解的方法。

let rows = await Activities.findAll();
rows = JSON.stringify(rows);
rows = JSON.parse(rows);

行仅获取数据

相关问题