The error your getting is because you're mixing the firebase native api & angularfire.
selectedUser$: AngularFirestoreDocument<any>;
Calling .ref
on your AngularFirestoreCollection
converts it to type firebase.firestore.CollectionReference
That being said, there are 2 possibilities to solve your issue:
Using angularfire
:
I'm assuming your userCollection looks something like this: this.afs.collection<User>
. Since you're querying a collection, there is no way to ensure your query predicate uid == key
is unique in firebase. So you query the collection and limit()
the result. This will return you an array with one document. flatMap()
will return you a single user.
this.afs.collection<User>('users', ref => ref.where('uid', '==', key).limit(1))
.valueChanges()
.pipe(
flatMap(users=> users)
);
Using the firebase
native api:
const query = this.usersCollection.ref.where('uid', '==', key);
query.get().then(querySnapshot => {
if (querySnapshot.empty) {
console.log('no data found');
} else if (querySnapshot.size > 1) {
console.log('no unique data');
} else {
querySnapshot.forEach(documentSnapshot => {
this.selectedUser$ = this.afs.doc(documentSnapshot.ref);
// this.afs.doc(documentSnapshot.ref).valueChanges().subscribe(console.log);
});
}
});
This way it's a bit easier to chain multiple .where
clauses if it's needed
this.selectedUser$ = this.userCollection('yourCollection', ref => ref.where('uid', '==', key) )
? This will retrieve the document on valueChanges().subscribe() – Carnassial