В приведенном ниже коде я пытаюсь добиться следующего:
1. У меня есть база данных firestore, в которой есть коллекция Orders с полями данных, такими как время, представляющее ожидаемое время доставки, и временной интервал со значениями [«Top Priority», «Mid Priority», «Low priority»], используемый для установки приоритета заказ.
2. Я пытаюсь создать строку tfinal, в которой хранятся ордера, принятые до даты отображения в соответствии с приоритетом «Top Priority»> «Mid Priority»> «Low priority».
3. Затем добавьте заказы на текущую дату отображения, а затем все остальные заказы с ожидаемой датой доставки после сегодняшнего дня, представленной массивом signature = ["<", "==", ">"];.
4. Я пробовал асинхронное ожидание, обратные вызовы и обещания, но кое-как моя логика не работает.
5. Мои функции даты правильные. Конфигурация пожарного магазина также правильная.
var temp, date2, col, pdes, t2, i, pri, signature, j, cl, tfinal, sig, pri2;
date2 = todaysdate(3); //todays date generates date like 26-10-2018 =>20181026 the date stored in fire store is also in this format
temp = tfinal = '';
i = j = 0;
cl = ["red", "blue", "green"]; //setting boundatey colours for html cards to be generated
pri = ["Top Priority", "Mid Priority", "Low priority"];
signature = ["<", "==", ">"];
function returnstring(callback) {
temp = "";
for (i = 0; i < signature.length; i++) { // used to iterate the signature/signs
for (j = 0; j < pri.length; j++) { //used to iterate priority
sig = signature[i];
pri2 = pri[j];
console.log(sig + ',' + pri2);
console.log(i + ',' + j);
tfinal += callback(sig, pri2); //callback function to make individual database query used async wait as well
console.log(tfinal); //output blank
}
}
$("#add1").append(tfinal);//jquery append html cards
}
returnstring(gettemp); //calling the above function
function gettemp(sig, pri2) { //callback function here
db.collection("Orders").where("date", sig, date2).where("timeslot", "==", pri2).onSnapshot(function(querySnapshot) {
console.log("one"); //function test check points
querySnapshot.forEach(function(doc) {
console.log("two"); //exicution not taking place from this log
console.log(doc.data()); //to log out availibility
pdes = doc.data().productlist;
t2 = "";
col = (signature[i] == "<") ? "red" : cl[j];
for (i = 0; i < pdes.length; i++) {
t2 = pdes[i].product + ' ' + pdes[i].
description + ' ' + pdes[i].quantity + ' ' +
pdes[i].unit
}
console.log(col + " " + t2);
temp += `<div class="card" style="margin:solid 3px ${col}">
<div class="card-header">${doc.data().date}</div>
<div class="card-body">${doc.data().name}<br>${t2}<br>Status:${doc.data().status}<br>${doc.data().placeofsupply}</div>
<div class="card-footer"><button type="button" class="btn btn-success"id="${doc.data().oid}">Success</button></div>
</div>`;
});
});
console.log(temp); //return coming out as blank
return temp;
}