想分别组织属于特定用户的数据,聊天所属用户应该分别放在一个数组中怎么办?
用户表具有name
,email
,password
和active
聊天表中有message
senderId
,recipId
我已经尝试过
const message_res = await models.Message.findAll({where: {$or: [{recipId:[2,3,5,1]}, { senderId: [2,3,5,1]}]}});
我得到了这个输出
[
{
"id": 1,
"message": "some text message!!",
"senderId": 5,
"recipId": 2,
"created_at": "2018-12-07T05:52:24.461Z",
"updated_at": "2018-12-07T05:52:24.461Z",
"User": {
"id": 2,
"name": "deen",
"email": "deena@gmail.com",
"password": "$2a$$7trQczrSSYKUF7zWPlbnh58e",
"active": true,
"created_at": "2018-12-07T05:44:05.323Z",
"updated_at": "2018-12-07T05:44:05.323Z"
}
},
{
"id": 2,
"message": "message",
"senderId": 5,
"recipId": 1,
"created_at": "2018-12-07T05:52:39.578Z",
"updated_at": "2018-12-07T05:52:39.578Z",
"User": {
"id": 1,
"name": "james",
"email": "james@gmail.com",
"password": "$2a$10$XGU4XiKbzSqjjjYdNaAcG",
"active": true,
"created_at": "2018-12-07T05:44:05.323Z",
"updated_at": "2018-12-07T05:44:05.323Z"
}
},
{
"id": 3,
"message": "test1",
"senderId": 1,
"recipId": 2,
"created_at": "2018-12-07T05:53:22.827Z",
"updated_at": "2018-12-07T05:53:22.827Z",
"User": {
"id": 2,
"name": "deen",
"email": "deena@gmail.com",
"password": "$2a$$7trQczrSSYKUF7zWPlbnh58e",
"active": true,
"created_at": "2018-12-07T05:44:05.323Z",
"updated_at": "2018-12-07T05:44:05.323Z"
}
},
]
Expected output
[{
"id": 1,
"name": "james",
"email": "james@gmail.com",
"password": "$2a$10$XGU4XiKbzSqjjjYdNaAcG",
"active": true,
"created_at": "2018-12-07T05:44:05.323Z",
"updated_at": "2018-12-07T05:44:05.323Z",
"chat": [{
"id": 2,
"message": "message",
"senderId": 5,
"recipId": 1,
"created_at": "2018-12-07T05:52:39.578Z",
"updated_at": "2018-12-07T05:52:39.578Z",
},{
"id": 3,
"message": "test1",
"senderId": 1,
"recipId": 2,
"created_at": "2018-12-07T05:53:22.827Z",
"updated_at": "2018-12-07T05:53:22.827Z"
}
]
},{
"id": 2,
"name": "deen",
"email": "deena@gmail.com",
"password": "$2a$$7trQczrSSYKUF7zWPlbnh58e",
"active": true,
"created_at": "2018-12-07T05:44:05.323Z",
"updated_at": "2018-12-07T05:44:05.323Z"
"chat": [{
"id": 1,
"message": "some text message!!",
"senderId": 5,
"recipId": 2,
"created_at": "2018-12-07T05:52:24.461Z",
"updated_at": "2018-12-07T05:52:24.461Z",
},{
"id": 3,
"message": "test1",
"senderId": 1,
"recipId": 2,
"created_at": "2018-12-07T05:53:22.827Z",
"updated_at": "2018-12-07T05:53:22.827Z"
}
]
},
like i need user 5 details along with his chat]