the problem that im having is that when using functions that return a promise i cant get them to generate the HTML correctly ang therefor i am unable to place it in the correct location.
the code currently creates all the elements from createClientElement
but only populates the first one.
the code that calls the promises:
function previousOrders(){
for (let i=0; i< UniqueOrderIdList.length; i++) {
var orderItemsByIdURL = base_url + orderItemsPath + url_auth + "&order_id=" + UniqueOrderIdList[i];
var index = i;
function getSuccess(obj){
var data = obj.data;
console.log(data);
for (let i=0; i< data.length; i++){
var Widget_id = data[i].widget_id
var Number = data[i].number
var PencePrice = data[i].pence_price
var price = parseFloat(PencePrice/100).toFixed(2)
Promise.all([getProdDesc(Widget_id), getClientId(UniqueOrderIdList[index])])
.then((values) => {
createClientElement(values[1], index).then(test => {
console.log(`index: ${index}`);
console.log(`number: ${Number}`);
console.log(`productDsc: ${values[0]}`);
console.log(`ID:${values[1]}`);
console.log(`price: ${price}`);
var template2 =
`<div>
<p class="orderdsc" id="orderdsc_${i}">${Number+": "}${values[0]}
<span class="orderItemid" id="orderItemid_${i}">ID:${values[1]}</span>
<span class="orderPrice" id="orderPrice_${i}">£${price}</span>
</p>
</div>`
$(`#pastOrderItems_${index}`).append(template2);
},onError)
},onError);
}
}
$.ajax(orderItemsByIdURL, {type: "GET", data: {},success: getSuccess });
console.log("success");
}
}
the getProdDesc
funtion:
function getProdDesc(widgetId){
return new Promise((resolve, reject) => {
var prodDescURL = base_url + widgetsPath + widgetId + url_auth;
function getSuccess(obj){
if (obj.status == "success") {
console.log("Got Product:", obj.data);
var data = obj.data;
var description = data[0].description;
resolve(description);
} else if (obj.message) {
reject(new Error(obj.message));
} else {
reject(new Error("Invalid request"));
}
}
$.ajax(prodDescURL, {type: "GET", data: {},success: getSuccess });
});
}
the getClientId
function:
function getClientId(orderId){
return new Promise((resolve, reject) => {
setTimeout(()=> {
var prodDescURL = base_url + ordersPath + orderId + url_auth;
function getSuccess(obj){
if (obj.status == "success") {
console.log("Got Client: ", obj.data);
var data = obj.data;
var clientId = data[0].client_id;
resolve(clientId);
} else if (obj.message) {
reject(new Error(obj.message));
} else {
reject(new Error("Invalid request"));
}
}
$.ajax(prodDescURL, {type: "GET", data: {},success: getSuccess });
}, 200)
});
}
the createClientElement
funtion
function createClientElement(client, i){
return new Promise((resolve, reject) =>{
var clientURL = base_url + clientPath + client + url_auth
function getSuccess(obj){
var data = obj.data;
var client_name = data[0].name
var client_address = data[0].address
var client_phone = data[0].phone
var client_email = data[0].email
var template =
`<div class="clientMain">
<div id="clients">
<p class="client top" id="clientName">Client name: ${client_name}</p>
<p class="client" id="clientAddress">Client address: ${client_address}</p>
<p class="client" id="clientPhone">Client phone: ${client_phone}</p>
<p class="client bottom" id="clientEmail">Client email: ${client_email}</p>
</div>
<div class="order" id="pastOrderItems_${i}">
</div>
</div>
`
$("#pastOrdersINPUT").append(template);
resolve("success");
}
$.ajax(clientURL, {type: "GET", data: {},success: getSuccess });
});
}
I know that each function works individually and I’ve gone a little code blind trying to work this out.
reference for what the final HTML result should look like (variable inputs left in for ease of reference):
<div id="pastOrdersINPUT">
<div class="clientMain">
<div id="clients">
<p class="client top" id="clientName">Client name: ${client_name}</p>
<p class="client" id="clientAddress">Client address: ${client_address}</p>
<p class="client" id="clientPhone">Client phone: ${client_phone}</p>
<p class="client bottom" id="clientEmail">Client email: ${client_email}</p>
</div>
<div class="order" id="pastOrderItems_${i}">
<div>
<p class="orderdsc" id="orderdsc_${i}">${Number+": "}${values[0]}
<span class="orderItemid" id="orderItemid_${i}">ID:${values[1]}</span>
<span class="orderPrice" id="orderPrice_${i}">£${price}</span>
</p>
</div>
</div>
</div>
</div>
2
Answers
The function
getSuccess(obj)
inpreviousOrders()
referred toindex
, but by the time those ajax call succeed they would all get the sameindex
value becauseindex
has function scope, not block scope. Try changingvar index = i;
tolet index = i;
You should really consider using async await to make the code more readable and maintainable.
I have made some changes to make it run using a dummy endpoint, and marked some spots that were important.
Maybe you can try to do the changes in your code to see if it would work: