Node.js 如果include不';在sequelize中找不到任何关系数据
我试图找出一本书可以有多个横幅的书 所以我在找书的时候用了很多横幅 这是工作的罚款,但我希望反应是不同的,如果在案件书并没有任何横幅Node.js 如果include不';在sequelize中找不到任何关系数据,node.js,json,express,sequelize.js,Node.js,Json,Express,Sequelize.js,我试图找出一本书可以有多个横幅的书 所以我在找书的时候用了很多横幅 这是工作的罚款,但我希望反应是不同的,如果在案件书并没有任何横幅 Book.findAll({ order: [ ['updated_at', 'ASC'] ], attributes: { exclude: ["thumbnail",
Book.findAll({
order: [
['updated_at', 'ASC']
],
attributes: {
exclude: ["thumbnail", "listenTime", "created_at", "today_book_day"],
},
include: [{
model: banner,
as: 'banners',
attributes: [
[banner_attribute, 'url']
],
where: whereAttribute,
required: false
}
]
}).then(books => {
var totalPage = 1;
if (totalBooks){
totalPage = Math.ceil(totalBooks / pagesize);
}
var metaInfo = {
currentPage: parseInt(page),
totalPage: totalPage,
pagesize: pagesize
};
var output = {
'meta': metaInfo,
"data": books,
"status": 200
};
res.send(output);
});
得到如下响应:
{
"meta": {
"currentPage": 1,
"totalPage": 3,
"pagesize": 100
},
"data": [
{
"id": 114,
"rating": "5.00",
"like": -1,
"comment": 0,
"listen": 49,
"category": "[]",
"order": 9,
"updated_at": 1589475237,
"thumbnailUrl": null,
"banners": []
},
"status": 200
]
}
{
"meta": {
"currentPage": 1,
"totalPage": 3,
"pagesize": 100
},
"data": [
{
"id": 114,
"rating": "5.00",
"like": -1,
"comment": 0,
"listen": 49,
"category": "[]",
"order": 9,
"updated_at": 1589475237,
"thumbnailUrl": null,
"banners": [
{
"url" : null
}
],
},
"status": 200
]
}
但我希望JSON响应如下所示:
{
"meta": {
"currentPage": 1,
"totalPage": 3,
"pagesize": 100
},
"data": [
{
"id": 114,
"rating": "5.00",
"like": -1,
"comment": 0,
"listen": 49,
"category": "[]",
"order": 9,
"updated_at": 1589475237,
"thumbnailUrl": null,
"banners": []
},
"status": 200
]
}
{
"meta": {
"currentPage": 1,
"totalPage": 3,
"pagesize": 100
},
"data": [
{
"id": 114,
"rating": "5.00",
"like": -1,
"comment": 0,
"listen": 49,
"category": "[]",
"order": 9,
"updated_at": 1589475237,
"thumbnailUrl": null,
"banners": [
{
"url" : null
}
],
},
"status": 200
]
}
这是无效的JSON
"banners": [url" : null] // table of... what?
我猜你想要这样的东西
"banners": [ {"url" : null} ] // table with one object, which has key 'url' and value 'null'
但若banners数组为空,那个么您可以断定url为空。您不需要显式地放置此信息。好的,JSON无效,我已经修复了它。但是对于期望的响应,这是android开发者的要求,响应应该是这样的。如果是这样,那么我认为您必须添加一些逻辑(例如,在控制器中)以在发送响应之前更改该响应。