花药返回有效载荷中未获取返回值

时间:2019-04-25 11:13:50

标签: javascript angular typescript

我有一个问题,其中一个返回值的函数没有在另一个函数中通过。这是我的代码

public _getProfileToUpdate() {
  return {
    corporateId: this.storeService.setStoreData().profile.preferred_username[1],
    id: this.storeService.setStoreData().profile.sub,
    firstName: this.userFormGroup.controls.firstName.value,
    lastName: this.userFormGroup.controls.lastName.value,
    email: this.userFormGroup.controls.email.value,
    mobile: this.userFormGroup.controls.mobile.value,
    workNumber: this.userFormGroup.controls.workNumber.value,
    roleId: this.sortRoleId()
  };
}

sortRoleId() {
  this._contentService._getRoles().subscribe((resp) => {
    const search = this.storeService.setStoreData().profile.role;
    const index = Object.values(resp.roles).indexOf(search);
    const result = Object.keys(resp.roles)[index];
    return result;
  })
}

因此我试图将另一个“结果”值发送到另一个函数中的“ roleId”值,但显示为未定义。

2 个答案:

答案 0 :(得分:1)

尝试一下

public _getProfileToUpdate() {
    // Service call
    this._contentService._getRoles().subscribe((resp) => {
        const search = this.storeService.setStoreData().profile.role;
        const index = Object.values(resp.roles).indexOf(search);
        const result = Object.keys(resp.roles)[index];
        // Return object
        return {
            corporateId: this.storeService.setStoreData().profile.preferred_username[1],
            id: this.storeService.setStoreData().profile.sub,
            firstName: this.userFormGroup.controls.firstName.value,
            lastName: this.userFormGroup.controls.lastName.value,
            email: this.userFormGroup.controls.email.value,
            mobile: this.userFormGroup.controls.mobile.value,
            workNumber: this.userFormGroup.controls.workNumber.value,
            roleId: result
        };
    })
}

答案 1 :(得分:1)

我会在您的ngOnInit中订阅内容服务,或者会更改您方法的调用层次,例如:

profile = {};
   public _getProfileToUpdate() {
 this._contentService._getRoles().subscribe((resp) => {
 const search = this.storeService.setStoreData().profile.role;
        const index = Object.values(resp.roles).indexOf(search);
        const result = Object.keys(resp.roles)[index];
        // Return object
        this.profile = {
            corporateId: this.storeService.setStoreData().profile.preferred_username[1],
            id: this.storeService.setStoreData().profile.sub,
            firstName: this.userFormGroup.controls.firstName.value,
            lastName: this.userFormGroup.controls.lastName.value,
            email: this.userFormGroup.controls.email.value,
            mobile: this.userFormGroup.controls.mobile.value,
            workNumber: this.userFormGroup.controls.workNumber.value,
            roleId: result
        };
    })
}

您以表格或其他形式传递变量arround,并在变量更新后立即调用angular来调用处理信息所需的功能。