Javascript 无法识别Cloud Firestore document.get()和.where()函数

Javascript 无法识别Cloud Firestore document.get()和.where()函数,javascript,firebase,firebase-authentication,google-cloud-firestore,angularfire2,Javascript,Firebase,Firebase Authentication,Google Cloud Firestore,Angularfire2,我正在使用Firebase身份验证和Cloud Firestore构建基于权限的身份验证系统,在登录时检查用户文档时遇到问题。该应用程序使用Angular6和angularfire2构建 其想法是,一旦用户登录应用程序,就会检查是否已为该用户创建了用户文档。如果没有,它将创建一个用户文档,并用默认权限和用户信息填充它 以下是我的代码和我尝试过的两个函数: import { Injectable } from '@angular/core'; import { User } from './../

我正在使用Firebase身份验证和Cloud Firestore构建基于权限的身份验证系统,在登录时检查用户文档时遇到问题。该应用程序使用Angular6和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;
    }
}
从'@angular/core'导入{Injectable};
从“/../models/User.model”导入{User};
从“./permissions.service”导入{PermissionsService};
从'firebase/app'导入{auth};
从'angularfire2/auth'导入{AngularFireAuth};
进口{
安古拉火炉店,
AngularFirestoreDocument,
AngularFirestoreCollection,
}来自“angularfire2/firestore”;
从'rxjs'导入{可观察的};
从“rxjs/operators”导入{switchMap};
@注射的({
providedIn:'根',
})
导出类身份验证服务{
usersCollection=null;
用户:可观察;
建造师(
私人afAuth:AngularFireAuth,
私人数据库:AngularFirestore,
私人许可服务:许可服务,
) {
this.userscolection=db.collection('users');
this.user=this.afAuth.authState.pipe(
开关映射((用户)=>{
如果(用户){
返回这个.db
.doc(`users/${user.uid}`)
.valueChanges();
}否则{
返回(空);
}
}),
);
}
登录日志(){
const provider=new auth.GoogleAuthProvider();
返回此.oAuthLogin(提供者);
}
loginFacebook(){
const provider=new auth.FacebookAuthProvider();
返回此.oAuthLogin(提供者);
}
loginTwitter(){
const provider=new auth.TwitterAuthProvider();
返回此.oAuthLogin(提供者);
}
oAuthLogin(提供商){
返回此.afAuth.auth.signInWithPopup(提供程序)。然后((凭据)=>{
//我第一次尝试使用.where查找具有匹配电子邮件的文档
//这会给出一个错误,说明.where()不存在
const docExists=this.userscolection.where(
“电子邮件”,
'==',
credential.user.email,
);
如果(docExists){
console.log(“用户登录”);
}否则{
console.log('用户不存在');
this.createUser(credential.user);
}
//我第二次尝试使用.get()查找具有匹配uid的文档
//文档的id与身份验证用户的uid设计匹配
//这会导致一个错误,即.get()不存在
//这是一个.users集合
//.doc(凭证.user.uid)
//.get()
//。然后((docSnapshot)=>{
//if(docSnapshot.exists){
//console.log(“用户登录”);
//}其他{
//console.log('用户不存在');
//this.createUser(credential.user);
//  }
// });
});
}
createUser(用户){
log(“创建用户”);
const newUser:User={
uid:user.uid,
电子邮件:user.email,
photoURL:user.photoURL,
displayName:user.displayName,
角色:{
成员:是的,
},
权限:this.permissionsService.memberPermissions,
};
这是一个.users集合
.add(新用户)
。然后((docRef)=>{
log(“添加的新用户”);
newUser.uid=docRef.id;
docRef.set(新用户);
})
.catch((错误)=>{
log('添加用户时出错:'+err);
});
}
注销(){
this.afAuth.auth.signOut();
this.user=null;
}
}
以下是引发的错误:

zone.js:192未捕获类型错误:\ u this.userscolection.where不是函数

zone.js:192未捕获类型错误:\ u this.userscolection.doc(…).get是 不是功能

我对firebase非常陌生,尤其是Firestore(我想每个人都是),我在angularfire2或firebase的文档中找不到任何可以说明我为什么不能这样做的东西

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

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

//这会出现一个错误,说明.where()不存在:

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

可以通过以下方式查询集合:

const docExists=this.afs.collection('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');
                }
            });
        });
}
专用oAuthLogin(提供程序:任意){
返回this.afAuth.auth
.signInWithPopup(提供程序)
。然后(凭证=>{
const userscolection=this.afs.collection('users',ref=>ref.where('email','='credential.user.email));
const users=usersCollection.snapshotChanges()
.烟斗(
映射(操作=>{
返回actions.map(action=>{