Jm3s
Jm3s

Reputation: 617

deleting specific part of field in firebase (Angular)

I am trying to remove part of an object in firebase. This is what it looks like in the database:

enter image description here

this is within a specific user id inside a 'users' collection.

The way I am currently trying to delete it:

  removeUserLikedProperty(property_id: string) {
    console.log(property_id)
    return fromPromise(this.usersCollection.doc(`${this._auth.currentUserId}/test/${property_id}`).delete());
  }

this is within my service. Then I have this in my ts file:

  removeUserLikedProperty(property_id: string) {
    this._user.removeUserLikedProperty(property_id);
    console.log(property_id)
  }

and then finally call this on click in my html:

<button class="button button-previous" (click)="removeUserLikedProperty(property?.property_id)">unlike</button>

From everything that I have read, this is my understanding of how to delete a member of a field. By accessing the users collection, getting user id, then going into 'test' and then removing the specific id. Anyone have any further knowledge on this? May syntax may be completely off!

Upvotes: 1

Views: 935

Answers (1)

Nery Ortez
Nery Ortez

Reputation: 500

You're mixing documents and fields. Please Read the documentation about data model

Either way, from the image what I understand you're trying to achieve is to delete a field in a nested object.

You need to know two things: how to delete a field in a document and how to access a field in a nested object

The following should work:

removeUserLikedProperty(property_id: string) {
  console.log(property_id);
  return fromPromise(this.usersCollection.doc(`${this._auth.currentUserId}`).update({
      [`test.${property_id}`]: firebase.firestore.FieldValue.delete()
    })
  );
}

Upvotes: 2

Related Questions