0

I have one service, one component and one resolver as following. There are some codes in Service Constructor, it should set value in Service, then when I link to certain path, the Resolver of this route would call certain Service Method, and the Method should be able to read the value which already set by Constructor.

Service:

@Injectable({
  providedIn: 'root'
})
export class BuildjsonService {

  public currentUser: User;

  constructor() {
    console.log('BuildjsonService constructor');
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUser'));
   }


  genReqContent(fmCode: string, caseKey: string, transKey: string, content: string){
    
    let reqContent : ReqContent = new ReqContent();
    
    console.log('genReqContent=' + JSON.stringify(this.currentUser));
    
    reqContent.bankerId = this.currentUser.loginID;

    return reqContent;

  }

Click on certain route, rosolver fired:

@Injectable()
export class GetSubDeptResolver implements Resolve<any> {

  constructor(private httpClient: HttpClient,
              private buildJsonSrv: BuildjsonService){}

  resolve(): Observable<any> {

    <!-- calling service-->
    let reqCont = this.buildJsonSrv.genReqContent('', '', '', content);

    let reqJsonObj: ReqJsonObj = this.buildJsonSrv.genJsonReq(reqCont, new FlowContent(), new TxnInfoContent());
    return this.httpClient.post<any>('http://localhost:2387/utility/GetSubDept', reqJsonObj)

  }

}

However, when Resolver calling genReqContent() in Service, 'this.currentUser' is null.

I can see that this Service is loaded and constructor is called when App starts, so the currentUser should already has value, however it's gone when being used later.

Am I doing something wrong? Thank you.

2
  • Just wondering: You are not opening that link in a new tab or window, are you? Commented Jul 9, 2019 at 7:46
  • @Erbsenkoenig Thank you for reply. No, it's one the same tab. Commented Jul 9, 2019 at 7:59

1 Answer 1

2

It appears that sessionStorage does not contain a value for currentUser at the time your service is instantiated. You could change currentUser to a getter, which only calls the storage when it's actually needed. This would also give you the latest instance of currentUser, in case its value changes (e.g. as a result of token renewal by another service).

@Injectable({
  providedIn: 'root'
})
export class BuildjsonService {

  get currentUser() {
    const user = sessionStorage.getItem('currentUser');

    if (user === null) {
      throw new Error('No user found in sessionStorage.');
    }

    return JSON.parse(user) as User;
  }

  constructor() {
    console.log('BuildjsonService constructor');
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! After checking log one more time, this service indeed is instantiated before currentUser being add to the sessionStorage. Getter is great, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.