javascript - 返回值未在花药返回有效负载中拾取

标签 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”值,但它显示为未定义。

最佳答案

试试这个,

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/

相关文章:

typescript - 指定枚举类型

typescript - 为第三方库创建 typescript .d.ts

angular - 为什么第二次订阅会改变我的直播结果?

javascript - 有时机器人不会添加角色并设置昵称

javascript - 框中的 Google map 标记内容

javascript - 如何从 Angular 和 Typescript 中的选项卡数组中禁用选项卡元素

Angular 7 (RxJs 6.x) - 如何使用 throwError

javascript - webpack4 css 捆绑不适用于自定义样式名称

javascript - Angular $http.post 和 No 'Access-Control-Allow-Origin' header

angular - typescript 错误 "incorrectly implements interface"类型 'Subject<boolean>' 不可分配给类型 'Subject<boolean>'