Reputation: 5078
I am very new to Firebase. Why is it that I can't get what I am querying for. In my other queries, I can actually see the payload but with this one I can't. This is the schema for my database in firebase:
I need to get the data that has the date that I need. So this is how I created my query:
getSalesForToday() {
const data = this.fireStore.collection(
'Sales', ref => ref.where('TransactionDate', '==', formatDate(new Date, 'yyyy/MM/dd', 'en'))
);
console.log(data);
}
but it doesn't give me any result except for this:
AngularFirestoreCollection {ref: CollectionReference, query: Query, afs: AngularFirestore}
afs: AngularFirestore {scheduler: FirebaseZoneScheduler, firestore: Firestore, persistenceEnabled$: Observable}
query: Query {_query: Query, firestore: Firestore}
ref: CollectionReference {_query: Query, firestore: Firestore}
__proto__: Object
I need your help. Thank you.
Upvotes: 1
Views: 4144
Reputation: 367
const data = this.fireStore.collection(
'Sales', ref => ref.where('TransactionDate', '==', formatDate(new Date, 'yyyy/MM/dd', 'en'))
);
fireStore.collection
only returns an observable. You still need to subscribe to this observable to be able to use the data.
const data$ = this.fireStore.collection('Sales',
ref => ref.where('TransactionDate', '==', formatDate(new
Date,'yyyy/MM/dd', 'en')));
// Subscribing to collection observable to log out the data
data$.subscribe(data => console.log(data))
Keep in mind you need to unsubscribe this subscription whenever it is not in use.
Also check async pipe
for more info about the best practices of using observables in angular https://angular.io/api/common/AsyncPipe
Upvotes: 2
Reputation: 3576
You need to create and execute the query:
getSalesForToday() {
let query = ref.where('TransactionDate', '==',
formatDate(new Date, 'yyyy/MM/dd', 'en'))
.get()
.then(snapshot => {
if (snapshot.empty) {
console.log('No matching documents.');
return;
}
snapshot.forEach(doc => {
// Do something
console.log(doc);
});
})
.catch(err => {
console.log('Error getting documents', err);
});
}
ref must be the reference to the collection expected to contain the value.
Upvotes: 0