I have this firestore db that stores some data, but I have a problem when I try to fetch records from it.
My code is supposed to give a record of users, but gives the error:
ERROR FirebaseError: Expected type ‘Query’, but it was: a custom wh object.
here is my code
import { Firestore, addDoc, collection, collectionData } from '@angular/fire/firestore'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent {
constructor(private fs: Firestore) { }
addToFireStore(data: any) {
const collectionInstance = collection(this.fs, 'users')
addDoc(collectionInstance, data).then(() => {
console.log('saved successfully')
}).catch((error) => console.log(error))
}
getDataFromFireStore() {
const collectionInstance = collection(this.fs, 'users')
collectionData(collectionInstance).subscribe(val => console.log(val))
}
}
here is the package.json
{
"private": true,
"dependencies": {
...
"@angular/fire": "7.6.1",
"firebase": "^10.4.0",
"rxfire": "6.0.4",
"rxjs": "~7.5.0",
},
"devDependencies": {
"@angular-devkit/build-angular": "^15.0.3",
"@angular/cli": "^15.2.9",
"@angular/compiler-cli": "^15.0.0",
"typescript": "~4.8.2"
}
}
2
Answers
It is working using
getDoc()
, andgetDocs()
instead of usingcollectionData()
&docData()
Here’s how you can modify your getDataFromFireStore method to create a Firestore query and then use collectionData: