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

时间:2019-03-25 06:38:06

标签: javascript angular

我想要实现的是:
 -首先运行回调,然后
 -this.auth.javaAuthToken是另一个文件中的属性,被初始化为令牌的值。
 -之后,该函数称为:return this.auth.getUserFiles();

如果我这样做:

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

然后我得到一个错误:

  

函数必须返回一个值

所以我写了下面的代码来检查令牌是否存在。如果是这样,那么我调用该函数并返回。

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

运行程序时,
-首先打印outside if and fun
-然后打印令牌,然后
-然后inside fun被打印。

2 个答案:

答案 0 :(得分:2)

非常简单,当您尝试访问令牌(返回结果时)时,结果尚不可用。 在异步函数中,您必须等待值或订阅事件。

如果您订阅事件,则只能访问订阅中的值。

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

我想知道这是否也是异步的:

this.auth.getUserFiles();

然后您可以执行类似的操作

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

答案 1 :(得分:0)

尝试

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