Attempting to retrieve data from the database using the callback method "getAllOrdersByUserId". The output is displayed below:
[ TextRow {
DishOrderId: 163,
BagId: 'BPZDXT68148',
DateCreated: 2021-05-27T03:55:05.000Z,
Bags:
'[{"DishId":43,"DishName":"Kuchvi Biryani","Spicy":2,"UnitPrice":"6.99","Qty":5,"DishTotal":"34.95"}]',
},
TextRow {
DishOrderId: 162,
BagId: 'BNJENZ08608',
DateCreated: 2021-05-27T03:46:26.000Z,
Bags:
'[{"DishId":41,"DishName":"Dum Biryani","Spicy":2,"UnitPrice":"6.99","Qty":5,"DishTotal":"34.95"}, {"DishId":42,"DishName":"Tysoon Biryani","Spicy":2,"UnitPrice":"6.99","Qty":5,"DishTotal":"34.95"}',
} ]
Desiring to extract certain attributes from the above results in addition to "Bags". However, the challenge lies in fetching the URL for the image of the dishes, as it is not specified in "Bags". This requires parsing "Bags" and iterating through a loop to obtain the "DishId" that will be used to retrieve the URLs of the images from another method called getDishesByDishIds. The extracted DishIds are as follows:
[43]
[41, 42]
Executing the method getDishesByDishIds would yield the following results for the respective dish Ids when processed in loops:
[ TextRow {
DishId: 43,
DishImageUrl1:
'http://192.168.86.104:1111/images/dishImageUrl1_1602546024189.JPG'
} ]
[ TextRow {
DishId: 41,
DishImageUrl1:
'http://192.168.86.104:1111/images/dishImageUrl1_1602546024190.JPG'
},
TextRow {
DishId: 42,
DishImageUrl1:
'http://192.168.86.104:1111/images/dishImageUrl1_1602546024191.JPG'
} ]
Encountering difficulty in achieving the desired outcome as shown below. The main issue is the inability to include the URLs in the final result as depicted:
{
"success": 1,
"bag": [
{
"DishOrderId": 163,
"BagId": "BPZDXT68148",
"DateCreated": "2021-05-27T03:55:05.000Z",
"Bags": [
{
"DishId": 43,
"DishName": "Kuchvi Biryani",
"Spicy": 2,
"UnitPrice": "6.99",
"Qty": 5,
"DishTotal": "34.95"
}
],
"Url": ['http://192.168.86.104:1111/images/dishImageUrl1_1602546024189.JPG']
},
{
"DishOrderId": 162,
"BagId": "BNJENZ08608",
"DateCreated": "2021-05-27T03:46:26.000Z",
"Bags": [
{
"DishId": 41,
"DishName": "Dum Biryani",
"Spicy": 2,
"UnitPrice": "6.99",
"Qty": 5,
"DishTotal": "34.95"
},
{
"DishId": 42,
"DishName": "Tysoon Biryani",
"Spicy": 2,
"UnitPrice": "6.99",
"Qty": 5,
"DishTotal": "34.95"
}
],
"Url": ['http://192.168.86.104:1111/images/dishImageUrl1_1602546024190.JPG',
'http://192.168.86.104:1111/images/dishImageUrl1_1602546024191.JPG']
}
]
}
The comprehensive code is provided below:
myOrders: (req, res) => {
const userId = req.body.userId;
orderStatus = 2;
getAllOrdersByUserId(userId, orderStatus, (error, results) => {
if (error) {
console.log(error);
return res.status(500).json({
success: 0,
message: "Some other error",
error: error,
});
}
if (!results) {
return res.status(404).json({
success: 0,
message: "Record not found",
});
} else{
const myOrderArray = []; //this will be new array to store all details including dish Urls
console.log(results);
count = 0;
for (i = 0; i < results.length; i++) {
const myOrder = {}
myOrder.DishOrderId = results[i].DishOrderId;
myOrder.BagId = results[i].BagId;
myOrder.DateCreated = results[i].DateCreated;
myOrder.Bags = JSON.parse(results[i].Bags);
myOrderArray.push(myOrder);
OrderDishes = JSON.parse(results[i].Bags);
//for each one of dish in order dishes get the dish id
let dishId = []
DishUrls = [];
countz = 0;
for (j = 0; j < OrderDishes.length; j++) {
dishId.push(OrderDishes[j].DishId);
//fetch image url for the dish Id fetched
//DishUrls = [];
getDishesByDishIds(dishId, (error, getDishesByDishIdsResults) => {
if (error) {
console.log(error);
return res.status(500).json({
success: 0,
message: "Some other error",
error: error,
});
}
console.log(getDishesByDishIdsResults);
// DishUrls = [];
count2 = 0;
for (l = 0; l < getDishesByDishIdsResults.length; l++) {
count2++;
if(getDishesByDishIdsResults[l].DishImageUrl1 == undefined){
if(count2 == getDishesByDishIdsResults.length){
//proceed with other steps
console.log(DishUrls);
}
}
else{
DishUrls.push(getDishesByDishIdsResults[l].DishImageUrl1);
}
}
});
countz ++;
if(countz == OrderDishes.length){
//do next step
console.log(DishUrls);
myOrder.Url = DishUrls; //not working , coming incorrect
}
}
}
return res.json({
success: 1,
bag: myOrderArray,
});
}
});
},
};
Apologies for the lengthy post, but any assistance provided will be greatly appreciated as I have been stuck on this issue for a few days.