我是Node和JSON的新手,无法获得所需的JSON响应。我希望以有组织的分组方式将响应发送到应用程序。我已经尝试了几种方法来弄清楚如何进入数组和Json对象的数组,但是失败了。请帮忙。 我的代码如下:
models.sequelize.query("SELECT product_details.item_name, product_details.item_desc, product_details.current_price, store_details.store_name, store_details.google_place_id, store_details.store_address, offer_recs.offer_id, offer_recs.offer, offer_recs.createdAt FROM `offer_recs` LEFT OUTER JOIN scan_recs ON scan_recs.scan_id = offer_recs.scan_id LEFT OUTER JOIN store_details ON store_details.store_id = offer_recs.store_id LEFT OUTER JOIN product_details ON product_details.prod_id = scan_recs.prod_id LEFT OUTER JOIN buyer_acct_details ON buyer_acct_details.buyer_id = scan_recs.buyer_id WHERE buyer_acct_details.buyer_id="+req.user.buyer_id+" ORDER BY store_details.store_name ASC, product_details.item_name ASC", { type: models.sequelize.QueryTypes.SELECT })
.then(offers=>{
var store_name = '', tmp_store_name = '', offer_history = [], allOffers = [], i = 0, j=0;
offers.forEach(offer=>{
if (offer.store_name != null) {
if (i==0) {
tmp_store_name = offer.store_name;
store_name = offer.store_name;
offer_history.push({item_name: offer.item_name, store_name: offer.store_name});
allOffers.push({store_name: store_name, offers: offer_history});
} else {
if (i>0 && offer.store_name == tmp_store_name) {
offer_history.push({item_name: offer.item_name, store_name: offer.store_name});
} else {
if (i>0 && store_name[i]!=store_name[i-1]) {
allOffers.push({store_name: store_name[i-1], offers: offer_history})
offer_history=[];
store_name[i] = offer.store_name;
//offer_history.push({item_name: offer.item_name, item_desc: offer.item_desc, store_name: offer.store_name});
j++;
}
}
}
i++;
}
})
res.status(200).send({success: true, msg: "Successfully fetched offers", Result: allOffers});
});
我想要以以下方式进行JSON响应:
Result:
[
{
store_name: "Citimart",
offers:[
{
"item_name": "Television",
"item_desc": "LED Television",
"current_price": "24999.0000",
"store_name": "Citimart",
"google_place_id": "ChIJ8Xdmiwh3AjoRvzKnSWdFwbU",
"store_address": "3A, Humayan Place Lighthouse Building, New Market Area, Dharmatala, Taltala, Kolkata, West Bengal 700087, India",
"offer": "18500.0000",
"createdAt": "2018-07-20T17:03:20.000Z"
},
{
"item_name": "Toaster",
"item_desc": "Maharaja Toaster",
"current_price": "2999.0000",
"store_name": "Citimart",
"google_place_id": "ChIJ8Xdmiwh3AjoRvzKnSWdFwbU",
"store_address": "3A, Humayan Place Lighthouse Building, New Market Area, Dharmatala, Taltala, Kolkata, West Bengal 700087, India",
"offer": "2500.0000",
"createdAt": "2018-07-21T06:48:11.000Z"
}
]
},
{ store_name: "Walmart Delhi",
offers: [
{
"item_name": "Mobile",
"item_desc": "Samsung Smartphone J7",
"current_price": "11999.0000",
"store_name": "Walmart Delhi",
"google_place_id": "ChIJMcg5Nor8DDkR6hrTdF7Cido",
"store_address": "87, GT Road, New Seelampur, New Seelampur I, Shahdara, Delhi, 110053, India",
"offer": "1500.0000",
"createdAt": "2018-07-20T17:03:20.000Z"
},
{
"item_name": "Television",
"item_desc": "LED Television",
"current_price": "24999.0000",
"store_name": "Walmart Delhi",
"google_place_id": "ChIJMcg5Nor8DDkR6hrTdF7Cido",
"store_address": "87, GT Road, New Seelampur, New Seelampur I, Shahdara, Delhi, 110053, India",
"offer": "15000.0000",
"createdAt": "2018-07-20T17:04:05.000Z"
}
]
}
}