我試圖使用回呼方法 "getAllOrdersByUserId "從資料庫獲取資料。其結果如下:
[ TextRow {
DishOrderId: 163,
BagId: 'BPZDXT68148',
DateCreated: 2021-05-27T03:55:05.000Z。
Bags:
'[{"DishId":43, "DishName": "Kuchvi Biryani", "Spicy":2, "UnitPrice": "6.99", "Quantity":5, "DishTotal": "34.95"}',
},
TextRow {
DishOrderId: 162,
BagId: 'BNJENZ08608',
DateCreated: 2021-05-27T03:46:26.000Z。
包。
'[{"DishId":41, "DishName": "Dum Biryani", "Spicy":2, "UnitPrice": "6.99", "Quantity":5, "DishTotal": "34. 95"}, {"DishId":42,"DishName":"Tysoon Biryani","Spicy":2,"UnitPrice":"6.99","Qty":5,"DishTotal":"34.95"}',
} ]
我正試圖從上面的 "Bags "中獲取一些屬性。但是我想獲取的一個重要的東西是菜品圖片的URL,這在 "Baggs "中沒有指定,所以我正在決議 "Baggs "并通過回圈運行來獲取 "DishId",這將用于從另一個回呼名為getDishesByDishIds的方法中獲取圖片的URL。獲取的DishIds如下:
[43]
[41, 42]
當通過for回圈運行時,getDIshesByDishIds方法將為上述菜品Ids獲取以下結果:
[ 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'
} ]
我所面臨的真正問題是我沒有得到如下所示的確切結果。我面臨的主要問題是,即使我能夠獲取菜肴的URL,我也無法將Urls添加到最終結果中,如下所示:
{
"success"。1,
"bag": [
{
"DishOrderId": 163,
"BagId": "BPZDXT68148",
"DateCreated": "2021-05-27T03:55:05.000Z",
"Baggs": [
{
"DishId": 43,
"DishName": "Kuchvi Biryani",
"Spicy": 2,
"單位價格": "6.99",
"數量": 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,
"單位價格": "6.99",
"數量": 5,
"DishTotal": "34.95".
},
{
"DishId": 42,
"DishName": "Tysoon Biryani",
"Spicy": 2,
"單位價格": "6.99",
"數量": 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']
}
]
}
完整的代碼如下:
myOrders: (req, res) => {
const userId = req.body.userId。
orderStatus = 2;
getAllOrdersByUserId(userId, orderStatus, (error, results) =>/span> {
if (error) {
console.log(錯誤)。
return res.status(500).json( {
success: 0,
message: "一些其他錯誤"。
error: 錯誤。
});
}
if (! results) {
return res.status(404).json({
success: 0,
message: "未找到記錄"。
});
} 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(錯誤)。
return res.status(500).json( {
success: 0,
message: "一些其他錯誤"。
error: 錯誤。
});
}
console.log(getDishesByDishIdsResults)。
// DishUrls = [];
count2 = 0;
for (l = 0; l < getDishesByDishIdsResults.length; l ) {
count2 ;
if(getDishesByDishIdsResults[l].DishImageUrl1 == undefined){
if(count2 == getDishesByDishIdsResults.length){
//繼續進行其他步驟。
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 , come incorrect
}
}
}
return res.json({
success: 1,
bag: myOrderArray,
});
}
});
},
};
很抱歉發了這么長的帖子,但我真的很感謝那些想幫助我的人,因為我從幾天前就被困在這里了。
uj5u.com熱心網友回復:
免責宣告
請原諒我... 我在一天漫長的作業之后,在深夜寫了這篇文章......我對這篇文章沒有信心。
我對這段代碼沒有信心。它未經測驗,而且寫得很差。可能不會成功。
但是......
但是...
我希望它至少能讓你朝正確的方向發展。
重構后的代碼
span class="hljs-attr">myOrders。(req, res) => {
const userId = req.body.userId。
let orderStatus = 2;
getAllOrdersByUserId(userId, orderStatus, (error, results) =>/span> {
if (error) {
console.log(錯誤)。
res.status(500).json({
success: 0,
message: "一些其他錯誤"。
error: 錯誤。
});
return。
}
if (! results) {
res.status(404).json({
success: 0,
message: "未找到記錄"。
});
return;
}
for (let i = 0; i < results.length; i ) {
(() => {
let currentOrder = results[i];
currentOrder.Bags = JSON.parse(currentOrder.Bags)。
return new Promise((resolve, reject) => /span> {
let dishIds = [];
for (let j = 0; j < currentOrder.Bags.length; j ) {
dishIds.push(currentOrder.Bags[j].DishId) 。
}
(() => {
return new Promise((innerResolve, innerReject) =>/span> {
getDishesByDishIds(dishIds, (error, getDishesByDishIdsResults) => {
let dishUrlResults = [];
if (error) {
innerReject("在檢索菜品圖片urls時發生錯誤")。
return。
}
for (let l = 0; l < getDishesByDishIdsResults.length; l ) {
if (getDishesByDishIdsResults[l].DishImageUrl1 != undefined) {
dishUrlResults.push(getDishesByDishIdsResults[l].DishImageUrl1) 。
}
}
innerResolve(dishUrlResults)。
});
});
})()
.then((urlResults) => {
currentOrder.Url = urlResults;
resolve()。
})
.catch((innerPromiseError) => {
reject(innerPromiseError)。
});
});
})()
.then(() => {
res.json({
success: 1,
bag: results,
});
})
.catch((promiseError) =>/span> {
//Could not complete the request[/span]。
console.log(promiseError)。
res.status(500).json({
success: 0,
message: "一些其他錯誤"。
error: 錯誤。
});
});
}
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/332751.html
標籤:
