简体   繁体   English

如果永不运行,为什么会这样

[英]Why does this if never runs

What I am trying to achieve is: 我想要实现的是:
- first the callback runs and -首先运行回调,
- this.auth.javaAuthToken , which is a property in another file, gets initialized to the value of the token. this.auth.javaAuthToken是另一个文件中的属性,被初始化为令牌的值。
- after that, this function is called: return this.auth.getUserFiles(); -之后,将调用此函数: return this.auth.getUserFiles();

If I do this: 如果我这样做:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<any> {
   let token = null;
    this.auth.emailListCleaningUserDetail().subscribe((res: any)=>{
    token = res.body.data.access_token;
    token = this.auth.javaAuthToken;
     return this.auth.getUserFiles();
  });

Then I get an error: 然后我得到一个错误:

function must return a value 函数必须返回一个值

So I have written the code below to check if the token exists. 所以我写了下面的代码来检查令牌是否存在。 If so, then I call the function and return. 如果是这样,那么我调用该函数并返回。

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<any> {
   let token = null;
    this.auth.UserDetail().subscribe((res: any)=>{
    token = res.body.data.access_token;
    token = this.auth.javaAuthToken;
    console.log(token);
    console.log('inside fun');  
  });
    if(token) {
      console.log('inside if outside fun');
      this.auth.javaAuthToken = token;
      return this.auth.getUserFiles();
    }
    console.log('outside if and fun')
  }
}  

When I run the program, 当我运行程序时
- First outside if and fun is printed, -首先outside if and fun打印outside if and fun的事物,
- then the token is printed, and -然后打印令牌,并且
- then inside fun gets printed. -然后inside fun就被打印出来了。

Its quite simple, when you are trying to access token (when you return your result), the result is not yet available. 这很简单,当您尝试访问令牌(返回结果时)时,结果尚不可用。 In async functions you either have to await the value or subscribe to the event. 在异步函数中,您必须等待值或订阅事件。

If you subscribe to the event, you can only access the value inside the subscription. 如果订阅事件,则只能访问订阅中的值。

 resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<any> { let token = null; this.auth.UserDetail().subscribe((res: any)=>{ token = res.body.data.access_token; token = this.auth.javaAuthToken; console.log(token); console.log('inside fun'); // This is called when you receive your result which can be later as the code below as this is async }); // token is always null because its declared as null before // when this code runs, there is no result of your Service yet if(token) { console.log('inside if outside fun'); this.auth.javaAuthToken = token; return this.auth.getUserFiles(); } console.log('this runs right after the code before but the result is not available yet') } } 

im wondering if this is async too: 我想知道这是否也是异步的:

this.auth.getUserFiles();

You could then do something like this 然后你可以做这样的事情

 LoadUserData = async event => { var token = null; var userfiles = null; try { var response = await this.auth.UserDetail() if (response.data != null) { token = response.data.access_token; console.log('Receiving result: '+JSON.stringify(response.data)); } } catch (error) { console.log('ERROR: '+error) } try { var responseUserFiles = await this.auth.getUserFiles(); if (responseUserFiles.data != null) { // do something with your userfiles userfiles = responseUserFiles.data; console.log('Receiving result: '+JSON.stringify(responseUserFiles.data)); } } catch (error) { console.log('ERROR: '+error) } // Here you can return what you want, userfiles or token return userfiles; } 

try this 尝试这个

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<any> {
   let token = null;
    this.auth.UserDetail().subscribe((res: any)=>{
    token = res.body.data.access_token;
    token = this.auth.javaAuthToken;
    console.log(token);
    console.log('inside fun');  

    if(token) {
      console.log('inside if outside fun');
      this.auth.javaAuthToken = token;
      return this.auth.getUserFiles();
    }

 });
    console.log('outside if and fun')
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM