Javascript回调函数无法获取多个数据库请求

时间:2018-10-27 05:22:56

标签: javascript firebase asynchronous callback google-cloud-firestore

在下面给出的代码中,我试图实现以下目标:
1.我有一个firestore数据库,其中有一个Orders集合,其中的数据字段(如时间)表示预期的交付时间,并使用值[“ Top Priority”,“ Mid Priority”,“ Low priority”]设置时间的时隙订单。
2.我试图创建一个字符串tfinal,该字符串根据优先级“最高优先级”>“中级优先级”>“低优先级”保存显示日期之前的订单。
3.然后在显示的当前日期添加订单,然后添加其他所有具有今天之后的预计交货日期的订单,这些订单由array signature = [“ <”,“ ==”,“>”];表示。
4.我尝试了异步等待,回调和承诺,但是我的逻辑失败了。
5,我的日期功能正确,Firestore配置也正确。

  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;
}

0 个答案:

没有答案