我的应用程序是否等待可观察对象的完成启动主程序

时间:2019-07-18 12:59:44

标签: javascript angular google-cloud-firestore

在启动之前必须加载所有内容的全局服务

我已经更改了主要服务,路由文件,并删除了延迟加载。

import { Injectable } from "@angular/core";
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import { takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';

@Injectable({
    providedIn: 'root'
})

export class GlobalService {

    docUsuarioLogado;
    uidUsuarioLogado;
    emailUsuarioLogado;
    contaRef: string;
    contaId: string;
    contaPath: string;
    unsubscribeAll: Subject<any> = new Subject();


    constructor(private afAuth: AngularFireAuth, private afs: AngularFirestore) {}

    /* Set accont data */
    /* Set account data */
    async setAccountData(): Promise<void> {
        this.afAuth.auth.onAuthStateChanged(async user => {
            if (user) {
                console.log(user);
                this.uidUsuarioLogado = user.uid;
                this.docUsuarioLogado = this.afs.doc(`usuarios/${user.uid}`).valueChanges();
                this.emailUsuarioLogado = user.email;

                 this.afs
                    .doc(`usuarios/${user.uid}`)
                    .snapshotChanges()
                    .pipe(takeUntil( this.unsubscribeAll))
                    .subscribe(
                        (doc: any) => {
                            this.contaId = doc.payload.data().conta_ref.id;
                            this.contaRef = doc.payload.data().conta_ref;
                            this.contaPath = doc.payload.data().conta_ref;
                        },
                        error => {
                            console.log(error.message);
                        }
                    );
            }
        });
    }

    ngOnDestroy(): void {
        this.unsubscribeAll.next();
        this.unsubscribeAll.complete();
    }
}
import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection, DocumentData, AngularFirestoreDocument, DocumentReference } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { Ponto } from '../models/ponto.model';
import * as moment from 'moment';
import { GlobalService } from '../core/services/global.service';
import { FirestoreService } from '../core/services/firestore.service';

@Injectable()

export class MainService {

  dateTime = new Date();
  momentDate = moment(this.dateTime).format('YYYY-MM-DD');

  start = new Date(this.momentDate + ' 00:00');
  end = new Date(this.momentDate + ' 23:59');

getcurrentUserPonto() {         
              const marcacoesCollection = this.afs.collection('contas')
              .doc(this.globalService.contaId)
              .collection('marcacoes', ref => ref
                .where('datahora', '>', this.start)
                .where('datahora', '<', this.end)).valueChanges()
                return marcacoesCollection;
          }
}

注意:当我登录到应用程序时,它可以正常工作,但是当我已经在main中进行身份验证并且输入f5时,该值不会出现,this.globalService.contaId的值将为Undefined。

我正在使用Angle和Firestore。

控制台日志: 错误FirebaseError:函数CollectionReference.doc()要求其第一个参数的类型为非空字符串,但它是:undefined

0 个答案:

没有答案