简体   繁体   English

Sequelize js 'include' 和 'raw'

[英]Sequelize js 'include' and 'raw'

I have a relation between two entites like (every chest has one user)我有两个实体之间的关系(每个箱子都有一个用户)

entities.Chest.belongsTo(entities.User)

i want to retrieve all chests and their users in one query, so i do我想在一个查询中检索所有箱子及其用户,所以我这样做

entities.Chest.findAll({include:[{model: entities.User}]})

But i prefer to manipulate them as plain objects, i do但我更喜欢将它们作为普通对象来操作,我愿意

entities.Chest.findAll({raw:true, include:[{model: entities.User}]})

And the result does not include users at all, how can i achieve this?结果根本不包括用户,我怎样才能做到这一点?

as you see, raw has some problems with joins (there is an issue ) try just use instance method #toJSON如您所见,raw 在连接方面存在一些问题(存在问题)尝试仅使用实例方法#toJSON

entities.Chest.findAll({include:[{model: entities.User}]})
  .then(function(chestsSeq){
    var chests = chestsSeq.toJSON(); //same as chestsSeq.get({});
    //do something with raw chests object
  });

This syntax helps for me.这种语法对我有帮助。 You haven't to iterate your records.你不必迭代你的记录。 Just use nest: true and raw: true in pairs;只需使用nest: trueraw: true成对;

entities.Chest.findAll({
    raw:true,
    nest: true,
    include:[entities.User]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM