vlntn
vlntn

Reputation: 370

Get object from Firestore where isDeleted is false

I'm trying to get contact information from my Firebase Firestore database where the contact was not deleted yet but unfortunately my code wont work as expected.

object-details.page.ts

...
  contact: any = {};
...

    if (this.contactId && this.contactId != undefined && this.contactId != "") {
      this.angularFirestore
        .collection("accounts")
        .doc(this.userId)
        .collection("contacts", (ref) => ref.where("isDeleted", "==", false))
        .doc(this.contactId)
        .valueChanges()
        .subscribe((contact: any) => (this.contact = contact));
        console.log("this.contact", this.contact);
    } else {
      console.log("No contact data available");
    }

The code gets something as Subscriber but I'd need it as an object to get e.g. contact.contactName. Further the "where" statement does not filter the contacts correctly. Anyone an idea how to fix it?

console.log("this.contact", this.contact) console.log()

Contact Object Contact Object

Upvotes: 0

Views: 94

Answers (1)

vlntn
vlntn

Reputation: 370

Here is my solution:

    if (this.contactId && this.contactId != undefined && this.contactId != "") {
      this.angularFirestore
        .collection("accounts")
        .doc(this.userId)
        .collection("contacts", (ref) => ref.where("isDeleted", "==", false))
        .doc(this.contactId)
        .valueChanges()
        .subscribe((contact) => {
          this.contact = contact;
          if(this.contact.isDeleted == false){
          this.contact = contact;
          console.log("this.contact", this.contact);
          console.log("this.contact.isDeleted", this.contact.isDeleted);
          } else {
            this.contact = "";
          }
        });

Upvotes: 1

Related Questions