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

javascript - Sequelize js 'include' and 'raw' - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question asked Nov 29, 2016 at 20:25 Andrew McFinleyAndrew McFinley 1311 gold badge2 silver badges6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 13

This syntax helps for me. You haven't to iterate your records. Just use nest: true and raw: true in pairs;

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

as you see, raw has some problems with joins (there is an issue) try just use instance method #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
  });
发布评论

评论列表(0)

  1. 暂无评论