Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

angular - Trying to receive a service for make reading after

I'm trying to get code since frontend after of ask information to a apiRest endpoint, my problem is that I need to require an element into the information that I'm decoding of JSW token, but for some reason I can't asign the element, I think is something wrong whit my async/promise.

My error is being marked on tokeninfo.userId inside ngOninit and it says the next: Property 'UserId' does not exist on type 'Promise'

  TeamDataModel: TeamDataModel = new TeamDataModel();
  btnSubmit: HTMLButtonElement;
  UserModelData: UserModelData = new UserModelData();
  CompanyModelData: CompanyModelData = new CompanyModelData();



 ngOnInit(): void {
    this.InitForms();

 // Consiguiendo el ID del URL
    this._route.paramMap.subscribe((params => {
      if (params.has('CompanyId')) {
        this.CompanyModelData.CurrentCompanyId = params.get('CompanyId');
      }
    }));


const info: any = localStorage.getItem('_$');
const token = atob(info);
const tokeninfo = this.DecodedToken(token);
this.UserModelData.UserId = tokeninfo.UserId;
this.SelectCompanysByUserId(this.UserModelData.UserId);



   }


       async DecodedToken(token) {
  try {
    const res: ResponseModel = await this._login.DecodedToken(token).toPromise();
    if (res.Code == ECodeResponse.Ok){
     return token = res.Data;
    }

  }catch{}
}

if you need more information please ask for them

question from:https://stackoverflow.com/questions/65934232/trying-to-receive-a-service-for-make-reading-after

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Async functions always return a promise, so if you need the content, you will need to resolve the promise:

this.DecodedToken(token).then(tokeninfo => {
    this.UserModelData.UserId = tokeninfo.UserId;
    this.SelectCompanysByUserId(this.UserModelData.UserId);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...