Arrays 角度8为什么承诺返回未定义的值,尽管它';就在下决心之前,他得到了适当的安慰?

Arrays 角度8为什么承诺返回未定义的值,尽管它';就在下决心之前,他得到了适当的安慰?,arrays,angular,promise,resolve,Arrays,Angular,Promise,Resolve,编辑 如果脚本在以下位置输入addHousehouse()if条件,我会感到安慰: addHouseholdPromise.then((res) => { console.log("Promise HH: "+res) if (res != "add_hh_fail") { console.log("success: "+res) return res; } else { console.log

编辑

如果脚本在以下位置输入addHousehouse()if条件,我会感到安慰:

addHouseholdPromise.then((res) => {
      console.log("Promise HH: "+res)
      if (res != "add_hh_fail") {
        console.log("success: "+res)
        return res;
      }
      else {
        console.log("fail: "+res)
        // Add to addHousehold array log
        this.addHHArray(row)

      }
    })
它返回了值,但是当我在主函数中截取它时,它变成了未定义的

结束编辑

原创帖子

我有一个main函数,它从array循环一个数组
,它获取它的每一行并执行以下操作:

  • 使用
    this.api.postData(数组)
    将hoousehold添加到数据库中
  • 注册后,我们进入另一个循环,循环另一个数组
    toArray
    ,该数组将包含成员,并添加与行
    fromArray
    相关且具有以下条件的成员:

    if(行hh['hh\u new\u id']==行mbr['hh\u new\u id'])

  • 添加相关成员后,我们将他们注册到另一个计划
    program_to

  • 最后,我们获取每个
    hh\u id
    和每个相关的
    mbr\u id
    ,并通过
    addRelation()
    将它们连接起来

  • **一、主要方法:**

    uploadDataV3(fromArray, toArray) {
    
        let workBook = null;
        this.lengthFromArray = fromArray.length;
        this.lengthToArray = toArray.length;
        this.fromArrayUnsuccess = [];
        this.toArrayUnsuccess = [];
        this.arrayOfUnsuccessRelationInsert = [];
        this.fromArrayUnsuccessEnroll = [];
        this.toArrayUnsuccessEnroll = [];
        this.globalArray = [];
        let registered_hh_id = '';
        let registered_mbr_id = '';
        this.relationIDsArray = [];
        let myDate = new Date();
        let enrollDate = this.datePipe.transform(myDate, 'yyyy-MM-dd');
        let hh_id: any = '';
        let mbr_id: any = '';
        fromArray.forEach((row_hh, hh_idx) => {
          // Add household to household entity (table)
          hh_id = this.addHousehold(row_hh);
          console.log('HH ID: '+hh_id)
          // Enroll Household to program_from
          let hh_id_enroll_response = this.enrollHousehold(hh_id, row_hh);
          console.log('Enroll Response: '+hh_id_enroll_response)
          if (hh_id_enroll_response == true) {
            toArray.forEach((row_mbr, mbr_idx) => {
              if (row_hh['hh_new_id'] == row_mbr['hh_new_id']) {
                mbr_id = this.addMember(row_mbr);
                console.log('Member ID: '+mbr_id)
                // Enroll new member id to program_to
                let mbr_id_enroll_response = this.enrollMember(mbr_id, row_mbr);
                if (mbr_id_enroll_response == true) {
                  // Add relationship between each household and its related members
                  this.addRelation(hh_id, mbr_id);
                }
              }
            })
          }
        });
      }
    
    现在的主要问题在这一行:

    hh_id = this.addHousehold(row_hh);
    console.log('HH ID: '+hh_id)
    
    返回的值是未定义的,如下图所示:

    addhousehouse()
    方法中,它显示生成的id
    registered\u hh\u id

    console.log("check hh_id", this.registered_hh_id);
    

    即使在功能承诺的情况下:

    addHouseholdPromise.then((res) => {
      console.log("Promise HH: "+res)
      if (res != "add_hh_fail") {
        return res;
      }
      else {
        // Add to addHousehold array log
        this.addHHArray(row)
    
      }
    })
    
    此行显示返回的ID:

    console.log("Promise HH: "+res)
    
    因此,问题是addhousehouse方法中的ID是从服务器获取的,但它返回的是
    未定义的

    以下是主功能中使用的详细方法

    II。对于Add家庭:

    addHousehold(row) {
        console.log(row)
    
        let addHouseholdPromise = new Promise((resolve, reject) => {
          let attributes = [
    
            { attribute: "lhMhyp9F1kg", value: row["HH_last_name"] != "" ? row["HH_last_name"] : "" },
            { attribute: "v4nxGLRD3n8", value: row["HH_status"] != "" ? row["HH_status"] : "" },
            { attribute: "rgBt6xSDLPp", value: row["reason_of_inactivation"] != "" ? row["reason_of_inactivation"] : "" },
            { attribute: "w3VYFaC2tnF", value: row["old_lmms_id"] != "" ? row["old_lmms_id"].toString() : "" },
            { attribute: "TN0ZUAIq3jr", value: row["hh_new_id"] != "" ? row["hh_new_id"].toString() : "" },
            { attribute: "g8EjIWItCxm", value: row["registration_date"] != "" ? row["registration_date"] : "" },
          ]
          let data = {
            trackedEntityType: this.formGroup.controls.entity_from.value,
            orgUnit: this.formGroup.controls.organization.value,
            attributes: attributes
          }
          this.registered_hh_id = '';
          this.api.postData(data).subscribe(
            (response_hh) => {
              if (response_hh['httpStatus'] == "OK" && response_hh['httpStatusCode'] == 200 && response_hh['message'] == "Import was successful.") {
                this.registered_hh_id = response_hh['response']['importSummaries'][0]['reference'];
                console.log("check hh_id", this.registered_hh_id);
                resolve(this.registered_hh_id)
              }
              else {
                resolve("add_hh_fail");
              }
            },
            (error_hh) => {
              reject("add_hh_fail");
              console.log(error_hh)
            }
          );
        })
        addHouseholdPromise.then((res) => {
          console.log("Promise HH: "+res)
          if (res != "add_hh_fail") {
            return res;
          }
          else {
            // Add to addHousehold array log
            this.addHHArray(row)
    
          }
        })
      }
    
    **三、 登记家庭:**

    enrollHousehold(hh_id, row_hh) {
        let myDate = new Date();
        let result:boolean;
        let enrollDate = this.datePipe.transform(myDate, 'yyyy-MM-dd');
        let enrollHouseholdPromise = new Promise((resolve, reject) => {
          this.api.makeEnrollment(hh_id,
            this.formGroup.controls.program_from.value, "ACTIVE",
            this.formGroup.controls.organization.value,
            enrollDate, enrollDate)
            .subscribe(
              (response_hh_enroll) => {
                if (response_hh_enroll['httpStatus'] == "OK" && response_hh_enroll['httpStatusCode'] == 200 && response_hh_enroll['message'] == "Import was successful.") {
                  resolve("hh_enroll_success");
                }
                else {
                  reject("hh_enroll_fail");
                  // this.addHHArrayEnroll(row_hh)
                }
              },
              (error_hh_enroll) => {
                console.log(error_hh_enroll);
                // this.addHHArrayEnroll(row_hh)
                reject("hh_enroll_fail")
              }
            )
        })
        enrollHouseholdPromise.then((res) => {
          if (res == "hh_enroll_success") {
            result = true;
          }
          else {
            this.addHHArrayEnroll(row_hh);
            result = false;
            // Add to logs
          }
        })
    
        return result;
      }
    
    **四、 注册后,我们将添加成员:**

    addMember(row_mbr) {
        let mbr_attributes = [
    
          { attribute: "TN0ZUAIq3jr", value: row_mbr["hh_new_id"].toString() },
          { attribute: "YvQdThw2NdW", value: row_mbr["mbr_new_id"].toString() },
          { attribute: "jQrM04xaPxb", value: row_mbr["first_name"] != "" ? row_mbr["first_name"] : "" },
          { attribute: "CybsW0GygpD", value: row_mbr["last_name"] != "" ? row_mbr["last_name"] : "" },
          { attribute: "RwRUmhHpSKS", value: row_mbr["father_name"] != "" ? row_mbr["father_name"] : "" },
          { attribute: "wXbpKsIbSRH", value: row_mbr["mother_full_name"] != "" ? row_mbr["mother_full_name"] : "" },
          { attribute: "CtJzimjQTYJ", value: row_mbr["gender"] != "" ? row_mbr["gender"] : "" },
          { attribute: "CtWcqFJCFXV", value: row_mbr["relation_to_HH"] != "" ? row_mbr["relation_to_HH"] : "" },
          { attribute: "qaOqvRz6TNb", value: row_mbr["DOB"] != "" ? this.datePipe.transform(row_mbr["DOB"], "yyyy-MM-dd") : "" },
          { attribute: "lTjjW15jAmv", value: row_mbr["phone_number"] != "" ? row_mbr["phone_number"].toString() : "" },
          { attribute: "CGjfVDBVlqV", value: row_mbr["nationality"] != "" ? row_mbr["nationality"] : "" },
          { attribute: "yo33iKOsHfN", value: row_mbr["other_nationality"] != "" ? row_mbr["other_nationality"] : "" },
          { attribute: "lk1jkG6UK6a", value: row_mbr["unhcr_exists"] != "" ? row_mbr["unhcr_exists"] : "" },
          { attribute: "XskAQYfw4sx", value: row_mbr["unhcr_id"] != "" ? row_mbr["unhcr_id"] : "" },
          { attribute: "Olq1PTOXLSb", value: row_mbr["other_gov_id"] != "" ? row_mbr["other_gov_id"] : "" },
          { attribute: "E63TvSeUUnl", value: row_mbr["other_gov_id_number"] != "" ? row_mbr["other_gov_id_number"] : "" },
          { attribute: "v4nxGLRD3n8", value: row_mbr["mbr_status"] != "" ? row_mbr["mbr_status"] : "" },
          { attribute: "RbXBwAN4Fbq", value: row_mbr["reason_inactive"] != "" ? row_mbr["reason_inactive"] : "" },
          { attribute: "EN5E32lCYNa", value: row_mbr["coordinates"] },
          { attribute: "cWXekTraGOx", value: row_mbr["comments"] != "" ? row_mbr["comments"].toString() : "" }
        ]
        let mbr_data = {
          trackedEntityType: this.formGroup.controls.entity_to.value,
          orgUnit: this.formGroup.controls.organization.value,
          attributes: mbr_attributes
        }
    
        let addMemberPromise = new Promise((resolve, reject) => {
          this.api.postData(mbr_data).subscribe(
            (response_mbr) => {
              if (response_mbr['httpStatus'] == "OK" && response_mbr['httpStatusCode'] == 200 && response_mbr['message'] == "Import was successful.") {
                this.registered_mbr_id = response_mbr['response']['importSummaries'][0]['reference'];
                resolve(this.registered_mbr_id);
              }
              else {
                resolve("add_mbr_fail");
              }
            },
            (error_mbr) => {
              reject("add_mbr_fail");
              console.log(error_mbr)
            }
          );
        });
        addMemberPromise.then((res) => {
          if (res != "add_mbr_fail") {
            return res;
          }
          else {
            // Add to logs array
            this.addMbrArray(row_mbr);
          }
        })
      }
    
    **V.添加成员后,我们将其注册到计划中:**

    enrollMember(mbr_id, row_mbr) {
        let myDate = new Date();
        let result:boolean;
        let enrollDate = this.datePipe.transform(myDate, 'yyyy-MM-dd');
    
        let enrollMemberEnroll = new Promise((resolve, reject) => {
          this.api.makeEnrollment(mbr_id, this.formGroup.controls.program_to.value,
            "ACTIVE", this.formGroup.controls.organization.value,
            enrollDate, enrollDate)
            .subscribe(
              (response_mbr_enroll) => {
                if (response_mbr_enroll['httpStatus'] == "OK" && response_mbr_enroll['httpStatusCode'] == 200 && response_mbr_enroll['message'] == "Import was successful.") {
                  resolve("mbr_enroll_success");
                }
                else {
                  resolve("mbr_enroll_fail")
                }
    
              },
              (error_mbr_enroll) => {
                reject("mbr_enroll_fail");
                console.log(error_mbr_enroll)
              }
            );
        });
        enrollMemberEnroll.then((res) => {
          if (res == "mbr_enroll_success") {
            result = true;
          }
          else {
            // Add to enroll logs
            this.addMbrArrayEnroll(row_mbr);
            result = false;
          }
        })
    
        return result;
      }
    
    **六、 最后,我们添加了
    hh\u id
    和所有相关
    mbr\u id
    之间的关系:**

    addRelation(hh_id, mbr_id) {
        let myDate = new Date();
        let enrollDate = this.datePipe.transform(myDate, 'yyyy-MM-dd');
        let checkExistingRelationshipsPromise = new Promise((resolve, reject) => {
          this.api.existingRelationships(hh_id, this.formGroup.controls.program_from.value).subscribe(
            (relation) => {
              console.log(relation, relation['relationships'].length, this.relations)
              // if (relation['httpStatusCode'] == 200 && relation['httpStatus'] == 'OK') {
              if (relation['relationships'].length > 0) {
    
    
                this.relations = relation['relationships']
    
                resolve('relation_exists')
              }
              else {
                this.relations = [];
                resolve('no_relations')
              }
    
            }, (error) => {
              reject("rejected")
              console.log("relations api: " + error)
            })
        });
        checkExistingRelationshipsPromise.then((res) => {
          if (res == "rejected") {
            console.log("error occured while getting existing relations")
          }
          else {
            console.log(hh_id, mbr_id)
    
    
          }
    
          let addRelationshipPromise = new Promise((resolve, reject) => {
            let programOwners = [{
              ownerOrgUnit: this.formGroup.controls.organization.value,
              program: this.formGroup.controls.program_from.value,
              trackedEntityInstance: hh_id
            }];
            let relationships: any[] = []
            if (res == "relation_exists") {
              this.relations.forEach((val, idx) => {
                relationships.push(val)
              })
              // relationships.push(this.relations)
              console.log(relationships)
            }
            relationships.push(
              {
                lastUpdated: this.datePipe.transform(myDate, 'yyyy-MM-ddTHH:mm:ss'),
                created: this.datePipe.transform(myDate, 'yyyy-MM-ddTHH:mm:ss'),
                relationshipName: "LBN_HH_MBR_Relationship",
                bidirectional: false,
                relationshipType: this.formGroup.controls.relation_id.value,
                // relationship: "wMECqtsc47R",
                from: {
                  trackedEntityInstance: {
                    trackedEntityInstance: hh_id,
                    programOwners: []
                  }
                },
                to: {
                  trackedEntityInstance: {
                    trackedEntityInstance: mbr_id,
                    programOwners: []
                  }
                },
                // relationshipType: this.formGroup.controls.relation_id.value
              }
            );
    
            let data = {
              created: this.datePipe.transform(myDate, 'yyyy-MM-ddTHH:mm:ss'),
              orgUnit: this.formGroup.controls.organization.value,
              createdAtClient: this.datePipe.transform(myDate, 'yyyy-MM-ddTHH:mm:ss'),
              trackedEntityInstance: hh_id,
              lastUpdated: this.datePipe.transform(myDate, 'yyyy-MM-ddTHH:mm:ss'),
              trackedEntityType: this.formGroup.controls.entity_from.value,
              lastUpdatedAtClient: this.datePipe.transform(myDate, 'yyyy-MM-ddTHH:mm:ss'),
              inactive: false,
              deleted: false,
              featureType: "NONE",
              programOwners: programOwners,
              relationships: relationships
              // attributes: attributes
            }
            this.relationIDsArray.push(
              {
                hh_id: hh_id,
                mbr_id: mbr_id,
                data: data
              }
            )
            // this.addRelation(response_hh['response']['importSummaries'][0]['reference'], response_mbr['response']['importSummaries'][0]['reference'], data);
            this.api.addRelation(hh_id, data).subscribe(
              (response_relation) => {
                if (response_relation['httpStatusCode'] == 200 && response_relation['httpStatus'] == "OK"
                  && response_relation['message'] == 'Import was successful.') {
                  resolve("relation added");
                }
                else {
                  resolve("relation was not added");
                  this.addUnsuccessRelationInsert(hh_id,
                    mbr_id,
                    this.formGroup.controls.entity_from.value, this.formGroup.controls.organization.value, this.formGroup.controls.relation_id.value)
                }
    
              },
              (error) => {
                console.log(error);
                this.addUnsuccessRelationInsert(hh_id,
                  mbr_id,
                  this.formGroup.controls.entity_from.value, this.formGroup.controls.organization.value, this.formGroup.controls.relation_id.value)
                return false;
              }
    
            );
    
          })
          addRelationshipPromise.then((res) => {
            if (res == "relation added") {
              console.log("Relation Added")
            }
            else {
              console.log("Error while adding the relationship")
            }
          })
    
        })
      }
    

    您正在使用承诺的回调方法
    then()
    callback中的return关键字返回值。那不行。在
    then()
    中传递的函数将在将来某个时候在承诺得到解决时执行。因此,如果您想等待您的承诺是否会解析为获取值,请使用ES6
    async wait
    方法

    async addhouse(行){
    console.log(行)
    let addHouseholdPromise=新承诺((解决、拒绝)=>{
    //你的逻辑是这样的
    };
    //继续您的逻辑,但不要根据您的承诺调用then()方法。相反,让我们等待您的承诺得到解决
    让res=等待承诺;
    log(“承诺HH:+res”)
    如果(res!=“添加失败”){
    返回res;
    }
    否则{
    //添加到addHousehouse数组日志
    this.addHHArray(行)
    //在其他情况下也要归还一些东西
    }
    }
    
    请注意,函数的前缀是一个名为
    async
    的关键字。如果要使用
    wait
    语法等待承诺解决,这一点很重要

    让我们在main方法中添加
    asyncwait
    关键字以获取值

    异步上传数据v3(从阵列到阵列){
    //你的逻辑是这样的
    让res=等待这个。addhousehouse(row);
    log('result:',res);
    }
    
    现在,您的主要方法还将等待承诺得到解决,然后获取可用于进一步处理的值

    现在,以类似的方式修改其他方法,以等待承诺得到解决


    有关ES6承诺和异步等待关键字的更多信息,请参阅此。

    frm承诺返回的值现在类似于
    \uuuu zone\u symbol\uuu value
    \uuuuu zone\u symbol\uu state
    在名为
    ZoneAwarePromise
    的内容中确保已将
    异步
    关键字添加到所有使用
    的方法中它里面有
    关键字。