0

The API returns me values like:

{bookmarked: 'false'}

or

{bookmarked: 'true'}

The code is:

addToBookmark(): void {
    if (this.bookmarked) {
      this.bookmarkSubs = this.bookmarksService
        .removeFromBookmark(this.id)
        .subscribe((bookmarked: boolean) => {
          this.bookmarked = bookmarked;
          console.log(this.bookmarked);
        });
    } else {
      this.bookmarkSubs = this.bookmarksService
        .addToBookmark(this.id)
        .subscribe((bookmarked: boolean) => {
          this.bookmarked = bookmarked;
          console.log(this.bookmarked);
        });
    }
  }

When I show the bookmarked value in the page (just for testing purposes) like {{ bookmarked }} the value is [object Object]

I need to display 2 different icons based of the values true or false.

HTML code:

      <button
          class="btn btn-trans tooltip bookmark"
          (click)="addToBookmark()"
      >
          Bookmark
          <fa-icon [icon]="faBookmarkRegular" *ngIf="!bookmarked"></fa-icon>
          <fa-icon [icon]="faBookmarkSolid" *ngIf="bookmarked"></fa-icon>
      </button>

I can't directly get true or false from the bookmarked value from API, because it is an object. How can I transform this object to true or false, so I can show the right icons in the web page?

Thanks to everyone

3
  • 1
    looks like bookmarked is an object property not an object. So, you need to use object_name.bookmarked like this. Commented Sep 5, 2022 at 6:01
  • Thank you! it works. If you think my question can help someone, feel free to upvote it :D Commented Sep 5, 2022 at 6:04
  • It doesn't help someone trust me. Anyway happy learning. Commented Sep 5, 2022 at 6:09

3 Answers 3

2

It is because you are trying to print an object in the template. Also, the bookmarked value is a string(a non-empty string is always true), not a boolean change it to boolean.

addToBookmark(): void {
    if (this.bookmarked) {
      this.bookmarkSubs = this.bookmarksService
        .removeFromBookmark(this.id)
        .subscribe((bookmarked: boolean) => {
          this.bookmarked = stringToBoolean(bookmarked?.bookmarked)
          console.log(this.bookmarked);
        });
    } else {
      this.bookmarkSubs = this.bookmarksService
        .addToBookmark(this.id)
        .subscribe((bookmarked: boolean) => {
          this.bookmarked = bookmarked;
           this.bookmarked = stringToBoolean(bookmarked?.bookmarked);
          console.log(this.bookmarked);
        });
    }
  }

create a function convert string to the boolean method in util:

const stringToBoolean = key => key === 'true' ? true: false;

You don't need to make changes to your template.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like bookmarked is an object property and not an object or variable.

So, you need to use object_name.bookmarked like this so it works.

OR

You can destructure the object like this.

const {bookmarked} = apiResponseData;

Now you can use bookmarked.

Comments

0

Your response is an object so a small change in your function, renamed the 'bookmarked' property to 'res' inside the subscribe arguments to avoid the similar name confusion, Now you can access the 'bookmarked' property from the response object like below:

addToBookmark(): void {
if (this.bookmarked) {
  this.bookmarkSubs = this.bookmarksService
    .removeFromBookmark(this.id)
    .subscribe((res: any) => {
      this.bookmarked = res.bookmarked;
      console.log(this.bookmarked);
    });
} else {
  this.bookmarkSubs = this.bookmarksService
    .addToBookmark(this.id)
    .subscribe((res: any) => {
      this.bookmarked = res.bookmarked;
      console.log(this.bookmarked);
    });
 }
}

As soon as the component variable 'this.bookmarked' value will change and condition on template satisfies, that icon will appear on the screen.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.