从一个Firestore文档中检索数据以查询另一个

时间:2020-10-27 12:39:29

标签: javascript google-cloud-firestore

我在Firestore集合中有2个文档。

我想查询一个文档以获取用户的车牌,然后我希望使用该车牌检索存储在另一文档中的车辆数据。

这是我的代码:

function get_full_name_by_nplate(nplate){
    var docRef = global_db.collection("customer_data").doc('web-app-vehicle-data');
    docRef.get().then(function(doc) {
        var data = doc.data();
        given_name = data["vehicles"][nplate]["policy_holder_given_name"];
        family_name = data["vehicles"][nplate]["policy_holder_family_name"];
    
        console.log("given name: " + given_name); //this prints correctly! ==> "Jaime"
    
        return given_name + " " + family_name;//this returns undefined
    });
}

//load user data ////driving scores from backend
function loadUserData(user){
    var nplate_list;

    var docRef = global_db.collection("customer_data").doc('web-app-user-data');
    docRef.get().then(function(doc) {
        var data = doc.data();
        user_data = data["users"][user.email];
        nplate_list = user_data["number_plates"];
    
    }).then(function(){

        for(nplate of nplate_list){
        
            get_full_name_by_nplate(nplate).then(full_name => {
                console.log(full_name);//this prints undefined
            });
        }
    });
}

我已经阅读了How to return value from firestore database?的答案,并且我了解Firestore返回了Promise,所以在Firestore数据检索之后,我总是不得不使用.then

尽管我能够将正确的given_name打印到控制台,但是函数function get_full_name_by_nplate返回undefined

也许是行不通的是我将一个诺言嵌套在另一个诺言中,但是我不知道将它们嵌套是不是错误的,也不知道如何不嵌套而做。

更新

有了@kvetis的回答,我改进了代码,现在可以使用了。但是仍然感觉不对,我必须将nplate_list作为参数从一个.then()传递到下一个参数。.我不想想象是否必须将其传递5次,此外我仍在嵌套2条.then()语句,这也感觉很不自然。

//load user data ////driving scores from backend
function loadUserData(user){



var docRef = global_db.collection("customer_data").doc('web-app-user-data');
    docRef.get().then(function(doc) {
        var data = doc.data();

        global_user_data = data["users"][user.email];
        //console.log(global_user_data["number_plates"]);

        var user_name  = global_user_data["name"] + " " + global_user_data["surname"];
        document.getElementById("welcome_div").innerHTML = user_name;

        var nplate_list = global_user_data["number_plates"];
        
        console.log("these are the nplates: " + nplate_list);
        
        return nplate_list;
    })
    
    .then(function (nplate_list) {
        var docRef = global_db
            .collection("customer_data")
            .doc("web-app-vehicle-data");
        
        docRef.get().then(function(doc) {
            global_vehicle_data = doc.data()["vehicles"];
            
            var cust_dropdown = document.getElementById("cust-search-dropdown");
            dropdown_content = "";
            
            for(nplate of nplate_list){
                //console.log(nplate);
                var full_name = get_full_name_by_nplate(global_vehicle_data, nplate);
                dropdown_content += "<div class=\"cust-dropdown-items\"><a href=\"#\" id=\"" + nplate + "\" class=\"number-plate-links\">" + nplate + " " + full_name + "</a></div>";
            }
            
            cust_dropdown.innerHTML = dropdown_content;
            addListeners();
            
            // If user has only 1 vehicle then pre-load it
            if(nplate_list.length == 1){
                select_customer_express(nplate_list[0]);
            }
        });
    });
}

1 个答案:

答案 0 :(得分:2)

问题在于您可能在所有诺言完成之前就读过dropdown_content。您必须等待所有get_full_name_by_nplate完成。

不幸的是,您的代码是这样编写的,以便您一次又一次下载车辆数据。

最好的方法是将两个文档加载一个接一个地链接。否则,它会很慢,而且成本很高,因为您要按单据费用计费。

function get_full_name_by_nplate(data, nplate) {

    given_name = data["vehicles"][nplate]["policy_holder_given_name"];
    family_name = data["vehicles"][nplate]["policy_holder_family_name"];

    return given_name + " " + family_name;
  
}

//load user data ////driving scores from backend
function loadUserData(user) {
  var nplate_list;

  var docRef = global_db.collection("customer_data").doc("web-app-user-data");
  docRef
    .get()
    .then(function (doc) {
      var data = doc.data();
      user_data = data["users"][user.email];
      nplate_list = user_data["number_plates"];
    })

    .then(function () {
      var docRef = global_db
        .collection("customer_data")
        .doc("web-app-vehicle-data");
      return docRef.get();
    })

    .then(function (doc)  { return doc.data()})

    .then(function (vehicle_data) {
      for (nplate of nplate_list) {
       var full_name=  get_full_name_by_nplate(vehicle_data,nplate);
          dropdown_content +=
            '<div class="cust-dropdown-items"><a href="#" id="' +
            nplate +
            '" class="number-plate-links">' +
            nplate +
            " " +
            full_name +
            "</a></div>";
        
      }
      // now you do things with `dropdown_content`
    });
}