如何解决sequelize连表查询嵌套太深的问题,急急急
多表连接查询数据嵌套过深有没有遇到? 以前是连四张表成功时返回的数据。 大致一张发布表,一张评价表(谁回复谁),一张用户表,一张主题表。 有的对象其实不想嵌套的。比如 image.png 通过uid查找谁发布的,user对象里边的内容能否直接解放出去?
同理topicId查找到的主题 image.png 在comment评价中还有好几个一样的嵌套。救救 有没有什么方法合并,解决嵌套对象。???
{
"infoId": 1,
"uid": 1,
"topicId": 1,
"content": "1",
"like_count": 21,
"post_count": 1,
"updatedAt": "2018年06月06日T07:10:33.000Z",
"user": {
"uid": 1,
"username": "陈二",
"avatar": "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIHmiadgySExiakKxsHobq70sxZDyic1BORkNqJ5nezISMeZoh1om6ysG53dbAoYjK0FetXV83O8icUiaw/132"
},
"topic": {
"topicId": 1,
"title": "孤独",
"des": "你有朋友吗,有那种可以深夜想聊天,一个电话就能找到的朋友。没有吧"
},
"comments": [
{
"id": 2,
"infoId": 1,
"from_uid": 1,
"to_uid": 1,
"topic_id": 1,
"content": "nidayede",
"from": {
"uid": 1,
"username": "陈韦4"
},
"to": {
"uid": 1,
"username": "六六"
}
},
{
"id": 3,
"infoId": 1,
"from_uid": 1,
"to_uid": 1,
"topic_id": 1,
"content": "nidayede",
"from": {
"uid": 1,
"username": "琪琪"
},
"to": {
"uid": 1,
"username": "让她"
}
},
{
"id": 1,
"infoId": 1,
"from_uid": 1,
"to_uid": 2,
"topic_id": 1,
"content": "1",
"from": {
"uid": 1,
"username": "哈哈"
},
"to": {
"uid": 2,
"username": "陈"
}
}
],
"thumbsUps": [
{
"id": 1,
"infoId": 1,
"uid": 1,
"user": {
"uid": 1,
"avatar": "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIHmiadgySExiakKxsHobq70sxZDyic1BORkNqJ5nezISMeZoh1om6ysG53dbAoYjK0FetXV83O8icUiaw/132"
}
},
{
"id": 2,
"infoId": 1,
"uid": 1,
"user": {
"uid": 1,
"avatar": "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIHmiadgySExiakKxsHobq70sxZDyic1BORkNqJ5nezISMeZoh1om6ysG53dbAoYjK0FetXV83O8icUiaw/132"
}
}
]
}
11 回复
let data=[{ user:1 }] let user={ 1:{} } 获取 user的数据 let uid=data[0].user console.log( user[uid])
这种?
来自酷炫的 CNodeMD
sequelize有一个属性是attributes
比如:
city.findOne({
attributes: {
include: [
[sequelize.literal('province.name'), 'province'],
],
},
where: { id: id },
include: [{
model: province
},
})
这样可以吧要的东西留着 , 不要的使用exclude或者attributes排除就行了