Javascript 在另一个返回有效负载中未拾取返回值

Javascript 在另一个返回有效负载中未拾取返回值,javascript,angular,typescript,Javascript,Angular,Typescript,我有一个问题,返回一个值的函数没有在另一个函数中执行。这是我的密码 public\u getProfileToUpdate(){ 返回{ corporateId:this.storeService.setStoreData().profile.preferred_用户名[1], id:this.storeService.setStoreData().profile.sub, firstName:this.userFormGroup.controls.firstName.value, lastNa

我有一个问题,返回一个值的函数没有在另一个函数中执行。这是我的密码

public\u getProfileToUpdate(){
返回{
corporateId:this.storeService.setStoreData().profile.preferred_用户名[1],
id:this.storeService.setStoreData().profile.sub,
firstName:this.userFormGroup.controls.firstName.value,
lastName:this.userFormGroup.controls.lastName.value,
电子邮件:this.userFormGroup.controls.email.value,
mobile:this.userFormGroup.controls.mobile.value,
工作编号:this.userFormGroup.controls.workNumber.value,
roleId:这个。sortRoleId()
};
}
sortRoleId(){
此._contentService._getRoles().subscribe((resp)=>{
const search=this.storeService.setStoreData().profile.role;
常量索引=Object.values(resp.roles).indexOf(search);
常量结果=对象键(分别为角色)[索引];
返回结果;
})
}
因此,我试图将一个“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
        };
    })
}
试试这个

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
        };
    })
}

我会在您的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
        };
    })
}

您可以将变量以您的形式或任何形式传递到周围,并在其更新后立即调用处理您的信息所需的函数。

我将在您的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
        };
    })
}

您可以将变量以表单或其他形式传递到周围,并在其更新后立即调用处理信息所需的函数。

这是因为
\u contentService.\u getRoles()
是一个异步函数,因此
\u getProfileToUpdate()中的返回不会等待其订阅。因此,它将返回为
未定义的
。那么我该如何更改它?可以合并函数吗?是的,这很好,因为
\u contentService.\u getRoles()
是一个异步函数,因此
\u getProfileToUpdate()
中的返回不会等待其订阅。因此,它将返回为
undefined
。好的,那么我该如何更改它?可以组合函数吗?是的,这很好如果调用_getProfileToUpdate()可能在请求完成之前不会返回任何内容resolved@Daniel没错。我想这是预期的行为。填充对象字段需要服务调用的响应。@skydev它是否满足您的要求?如果调用_getProfileToUpdate(),则在请求完成之前可能不会返回任何内容resolved@Daniel没错。我想这是预期的行为。填充对象字段需要来自服务调用的响应。@skydev它是否满足您的要求?