Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular Firestore:检查数据是否存在,并基于该数据更新全局变量_Javascript_Angular_Typescript_Function_Google Cloud Firestore - Fatal编程技术网

Javascript Angular Firestore:检查数据是否存在,并基于该数据更新全局变量

Javascript Angular Firestore:检查数据是否存在,并基于该数据更新全局变量,javascript,angular,typescript,function,google-cloud-firestore,Javascript,Angular,Typescript,Function,Google Cloud Firestore,请求的行为: 我想创建一个AngularService,它检查某个文档是否存在,并根据结果更新一个全局变量 当前状态 该函数将成功检查文档是否存在。它还更新if/else语句中的全局变量 问题 即使第一部分工作正常,它也总是返回“undefined” 我怎样才能解决这个问题?是否与功能范围有关 我的服务: 导出类配置文件followservice{ //应该更新的全局变量 followState:布尔值; 构造函数(专用angularFirestore:angularFirestore){}

请求的行为:
我想创建一个AngularService,它检查某个文档是否存在,并根据结果更新一个全局变量

当前状态
该函数将成功检查文档是否存在。它还更新if/else语句中的全局变量

问题
即使第一部分工作正常,它也总是返回“undefined”

我怎样才能解决这个问题?是否与功能范围有关

我的服务:

导出类配置文件followservice{
//应该更新的全局变量
followState:布尔值;
构造函数(专用angularFirestore:angularFirestore){}
checksFollow(followerID:string,followerID:string):布尔值{
常量followDoc=
this.angularFirestore.collection(`users/${followerID}/follower`).doc(followerID).ref;
followDoc.get()。然后((doc)=>{
如果(文件存在){
this.followState=true;
}否则{
this.followState=false;
}
});
返回此。followState;
}
}
followDoc.get()是一个返回承诺的异步函数。要返回更新后的this.followState,您必须等待
然后

一种方法是使用async/await

async checksFollow(followingID: string, followerID: string): boolean {
    const followDoc =
    this.angularFirestore.collection(`users/${followingID}/following`).doc(followerID).ref;

    return followDoc.get().then((doc) => {
      if (doc.exists) {
          this.followState = true;
      } else {
          this.followState = false;
      }

      return this.followState;
    }); 
  }
在代码的另一部分中,您可以调用
checks follow
将关键字
放入wait
并等待响应

async someMethodToCallChecksFollow() {
    const result = await this.checksFollow();
    console.log(result);
}
如果您想在html中使用响应,我建议将followState从primitive
boolean
更改为
BehaviorSubject
,然后调用
this.followState.next(true)

例如:

export class YourService {
  public followState = new BehaviorSubject<boolean>(false);

  async checksFollow(followingID: string, followerID: string): boolean {
    const followDoc =
    this.angularFirestore.collection(`users/${followingID}/following`).doc(followerID).ref;

    return followDoc.get().then((doc) => {
      if (doc.exists) {
          this.followState.next(true);
      } else {
          this.followState.next(false);
      }

      return this.followState.getValue();
    }); 
  }
}

这是一个良好的开端。但我在服务背后的想法是根据真或假在角度组件中显示一个按钮。如果我向someMethodToCallChecksFollow()添加一个return语句,并在角度组件中调用它,它将返回[object Promise]。我必须转换数据吗?如果是这样的话,你知道我能做什么吗?你可以使用异步管道,它将等待你的方法解析,但如果我想从组件调用该方法,我发现了这个错误:
类型“Promise”不能分配给类型“boolean”。
我合并了您的建议,并基于此提出了一个新问题:如果您能看一看,那就太棒了:)
<div *ngIf="yourServiceInstance.followState | async">It is true</div>