Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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/2/ionic-framework/2.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
服务速度比page | Ionic 4 | Firebase慢_Firebase_Ionic Framework_Google Cloud Firestore_Ionic4 - Fatal编程技术网

服务速度比page | Ionic 4 | Firebase慢

服务速度比page | Ionic 4 | Firebase慢,firebase,ionic-framework,google-cloud-firestore,ionic4,Firebase,Ionic Framework,Google Cloud Firestore,Ionic4,首先:一般来说,我对编码非常陌生。对不起,如果这个问题很愚蠢^^ 我试着用ionic和firestore构建一个简单的应用程序。 现在我想创建一些简单的服务。现在我遇到了一个问题,即服务在 已传递“this.userData=this.userService.currUser”。这意味着,我在这个.userData中没有数据 如何正确执行此操作?:/ 谢谢 服务: export class UserServiceService { public currUser: any; privat

首先:一般来说,我对编码非常陌生。对不起,如果这个问题很愚蠢^^ 我试着用ionic和firestore构建一个简单的应用程序。 现在我想创建一些简单的服务。现在我遇到了一个问题,即服务在 已传递“this.userData=this.userService.currUser”。这意味着,我在这个.userData中没有数据

如何正确执行此操作?:/

谢谢

服务:

export class UserServiceService {
  public currUser: any;
  private currUserRef: any;

  constructor(private afs: AngularFirestore, public afAuth: AngularFireAuth) {
    const currUserId = afAuth.auth.currentUser.uid;
    this.currUserRef = afs.collection('users').doc(currUserId).valueChanges();
    this.currUser = this.currUserRef.subscribe(data => {
      return data;
    });
  }
}
页面:

export class EventlistPage implements OnInit {

  private events: Observable<Event[]>;
  private userData;

  constructor(private eventService: EventService, private afAuth: 
  AngularFireAuth, public userService: UserServiceService) { }

  ngOnInit() {
    this.events = this.eventService.getEvents();
    this.userData = this.userService.currUser;
  }

}
导出类EventlistPage实现OnInit{
私人事件:可观察;
私有用户数据;
构造函数(私有eventService:eventService,私有afAuth:
AngularFireAuth,公共用户服务:用户服务服务{}
恩戈尼尼特(){
this.events=this.eventService.getEvents();
this.userData=this.userService.currUser;
}
}

将你的
可观察的
转换为
承诺
并使用
。然后()

所以你的服务是这样的

export class UserServiceService {
  public currUser: any;
  private currUserRef: any;
  constructor(private afs: AngularFirestore, public afAuth: AngularFireAuth) {
    const currUserId = afAuth.auth.currentUser.uid;
    this.currUserRef = afs.collection('users').doc(currUserId).valueChanges();
  }
  getDataFromFireStore(){
   return this.currUserRef.toPromise();
  }
}
在您的用户类中

export class EventlistPage implements OnInit {
  private events: Observable<Event[]>;
  private userData;
  constructor(private eventService: EventService, private afAuth: 
  AngularFireAuth, public userService: UserServiceService) { }

  ngOnInit() {
    this.events = this.eventService.getEvents();
    this.userService.getDataFromFireStore().then(data => this.userData = data);
  }

}
导出类EventlistPage实现OnInit{
私人事件:可观察;
私有用户数据;
构造函数(私有eventService:eventService,私有afAuth:
AngularFireAuth,公共用户服务:用户服务服务{}
恩戈尼尼特(){
this.events=this.eventService.getEvents();
this.userService.getDataFromFireStore()。然后(data=>this.userData=data);
}
}

将你的
可观察的
转换为
承诺
并使用
。然后()

所以你的服务是这样的

export class UserServiceService {
  public currUser: any;
  private currUserRef: any;
  constructor(private afs: AngularFirestore, public afAuth: AngularFireAuth) {
    const currUserId = afAuth.auth.currentUser.uid;
    this.currUserRef = afs.collection('users').doc(currUserId).valueChanges();
  }
  getDataFromFireStore(){
   return this.currUserRef.toPromise();
  }
}
在您的用户类中

export class EventlistPage implements OnInit {
  private events: Observable<Event[]>;
  private userData;
  constructor(private eventService: EventService, private afAuth: 
  AngularFireAuth, public userService: UserServiceService) { }

  ngOnInit() {
    this.events = this.eventService.getEvents();
    this.userService.getDataFromFireStore().then(data => this.userData = data);
  }

}
导出类EventlistPage实现OnInit{
私人事件:可观察;
私有用户数据;
构造函数(私有eventService:eventService,私有afAuth:
AngularFireAuth,公共用户服务:用户服务服务{}
恩戈尼尼特(){
this.events=this.eventService.getEvents();
this.userService.getDataFromFireStore()。然后(data=>this.userData=data);
}
}

嗯,我试过了。现在我得到了“无法读取未定义的”Nvm的属性'getUserData',它现在可以工作了。谢谢但在页面呈现时,数据仍然不可用。我想做一些类似于'ngIf=“userData.isAdmin”的事情。有什么办法吗?用一个新的div包装整个数据,然后放入
ngIf=Data
YEs,检查数据是否为空。但我无法解释为什么。它绝对是正确的“currUserId”。集合也是正确的,并且文档没有在您的用户类中清空。将ngOnInit包含的内容移动到构造函数并尝试打印数据,您得到了什么?嗯,我试过了。现在我得到了“无法读取未定义的”Nvm的属性'getUserData',它现在可以工作了。谢谢但在页面呈现时,数据仍然不可用。我想做一些类似于'ngIf=“userData.isAdmin”的事情。有什么办法吗?用一个新的div包装整个数据,然后放入
ngIf=Data
YEs,检查数据是否为空。但我无法解释为什么。它绝对是正确的“currUserId”。集合也是正确的,并且文档没有在用户类中清空。将ngOnInit包含的内容移动到构造函数中,然后尝试打印数据,得到什么?