Product.findAll({
attributes: ['id', 'product_name', 'product_price'],
include: [{
model: Shop,
attributes: ['id', 'shop_name'],
required: true
},
{
model: LikedUsers,
//get number of users liked for this product
//get first 4 users as an array
required: true
},
{
model: Comment,
//get number of comments for this product
}]
}).then(products => {
res.send(products);
}).catch(err => {
res.status(422).send(err.errors);
});
}
id,shopId,categoryId ....
userId,productId,
userId,productId
这将给我一系列产品对象。我想以这种方式格式化响应对象:
{
"id": 669,
"product_name": "Summer Fashion Brand Sexy Women Short",
"product_price": "150.00",
"shops":{
"id": 1,
"sho_name": "some shop"
},
"num_likedUsers" 30,
"liked_users": [
{
userid: i,
username: 'someuser',
profile: 'someImage'
},
{
userid: i,
username: 'someuser',
profile: 'someImage'
}
],
"num_comments": "0"
},