Cloud Firestore 文档 .get() 和 .where() 函数无法识别

Cloud Firestore document .get() and .where() functions not recognized

我正在使用 Firebase 身份验证和 Cloud Firestore 构建一个基于权限的身份验证系统,但我在登录时无法检查用户文档。该应用程序是使用 Angular 6 和 angularfire2 构建的。

想法是,一旦用户登录,应用程序将检查是否已为用户创建用户文档。如果没有,它将创建一个用户文档并在其中填充默认权限和用户信息。

这是我的代码和我尝试过的两个函数:

import { Injectable } from '@angular/core';
import { User } from './../models/user.model';
import { PermissionsService } from './permissions.service';

import { auth } from 'firebase/app';
import { AngularFireAuth } from 'angularfire2/auth';
import {
    AngularFirestore,
    AngularFirestoreDocument,
    AngularFirestoreCollection,
} from 'angularfire2/firestore';

import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';

@Injectable({
    providedIn: 'root',
})
export class AuthService {
    usersCollection = null;
    user: Observable<User>;

    constructor(
        private afAuth: AngularFireAuth,
        private db: AngularFirestore,
        private permissionsService: PermissionsService,
    ) {
        this.usersCollection = db.collection('users');
        this.user = this.afAuth.authState.pipe(
            switchMap((user) => {
                if (user) {
                    return this.db
                        .doc<User>(`users/${user.uid}`)
                        .valueChanges();
                } else {
                    return of(null);
                }
            }),
        );
    }

    loginGoogle() {
        const provider = new auth.GoogleAuthProvider();
        return this.oAuthLogin(provider);
    }

    loginFacebook() {
        const provider = new auth.FacebookAuthProvider();
        return this.oAuthLogin(provider);
    }

    loginTwitter() {
        const provider = new auth.TwitterAuthProvider();
        return this.oAuthLogin(provider);
    }

    oAuthLogin(provider) {
        return this.afAuth.auth.signInWithPopup(provider).then((credential) => {
// My first attempt using .where to find a doc with matching email
// This gives an error saying .where() does not exist 
            const docExists = this.usersCollection.where(
                'email',
                '==',
                credential.user.email,
            );
            if (docExists) {
                console.log('User logged in');
            } else {
                console.log('user does not exist');
                this.createUser(credential.user);
            }
//My second attempt using .get() to find a doc with matching uid
//The id of the doc matches the uid of the auth user by design
//This gives an error saying .get() does not exist
            // this.usersCollection
            // .doc(credential.user.uid)
            // .get()
            // .then((docSnapshot) => {
            //  if (docSnapshot.exists) {
            //      console.log('User logged in');
            //  } else {
            //      console.log('user does not exist');
            //      this.createUser(credential.user);
            //  }
            // });
        });
    }

    createUser(user) {
        console.log('creating user');
        const newUser: User = {
            uid: user.uid,
            email: user.email,
            photoURL: user.photoURL,
            displayName: user.displayName,
            roles: {
                member: true,
            },
            permissions: this.permissionsService.memberPermissions,
        };

        this.usersCollection
            .add(newUser)
            .then((docRef) => {
                console.log('added new user');
                newUser.uid = docRef.id;
                docRef.set(newUser);
            })
            .catch((err) => {
                console.log('Error adding user: ' + err);
            });
    }

    logout() {
        this.afAuth.auth.signOut();
        this.user = null;
    }
}

以下是抛出的错误:

zone.js:192 Uncaught TypeError: _this.usersCollection.where is not a function

zone.js:192 Uncaught TypeError: _this.usersCollection.doc(...).get is not a function

我是 firebase 的新手,尤其是 Firestore 的新手(我想每个人都是),我在 angularfire2 或 firebase 的文档中找不到任何可以说明我为什么不能这样做的内容。

请帮助我理解为什么这些函数被视为无效。

此外,如果您对我如何以更好的方式处理整个身份验证过程有任何提示或意见,请随时添加评论。这是我第一次尝试创建这样的身份验证服务。

// This gives an error saying .where() does not exist:

那是因为 .where() 在类型 AngularFirestoreCollection 上不存在。您在构造函数中通过 this.usersCollection = db.collection('users') 对其进行赋值。

可以这样查询集合:

const docExists = this.afs.collection<User[]>('users', ref => ref.where('email', '==', credential.user.email));

但这并不能解决您的问题。在前面的语句中,docExist 也将是一个 AngularFirestoreCollection,并且对 if (docExists) { 的检查将始终为真。因此,您要寻找的是一种基于查询来检查文档是否存在的方法:

private oAuthLogin(provider: any) {
    return this.afAuth.auth
        .signInWithPopup(provider)
        .then(credential => {
            const usersCollection = this.afs.collection<User[]>('users', ref => ref.where('email', '==', credential.user.email));
            const users = usersCollection.snapshotChanges()
                .pipe(
                    map(actions => {
                        return actions.map(action => {
                            const data = action.payload.doc.data();
                            const id = action.payload.doc.id;
                            return { id, ...data };
                        });
                    }),
                    take(1));

            users.subscribe(snap => {
                if (snap.length === 0) {
                    console.log('user does not exist');
                    this.createUser(credential.user);

                } else {
                    console.log('User logged in');
                }
            });
        });
}