我遇到一个问题,即返回值的函数未在另一个函数中被拉出。这是我的代码
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”值,但它显示为未定义。
最佳答案
试试这个,
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
};
})
}
关于javascript - 返回值未在花药返回有效负载中拾取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55847983/