Angular throws a TypeError when attempting to utilize both getter and setter methods simultaneously

I am experiencing an issue with using getter and setter to set a field from the web cache. When I attempt to use the setter, I encounter a TypeError. The specific error message is:

ERROR TypeError: this.saveCache is not a function

Below are the implementation of the getter and setter functions in question:

    get saveCache (): any {
        if (localStorage.getItem('saveCache') === null) {
          return null;
        }
        return JSON.parse(localStorage.getItem('saveCache'));
      }
      
      set saveCache(value:any) {
        localStorage.setItem('saveCache', JSON.stringify(value));
      }

Whenever I try to set the value for the saveCache field, the error mentioned above is triggered. How can I go about resolving this issue?

An example showcasing the error can be found here:

https://stackblitz.com/edit/angular-ivy-cryfvb?file=src%2Fapp%2Fapp.component.ts

Answer №1

To properly set a value, the following syntax must be used:

this.updateCache = this.books;

Avoid using this.updateCache(this.books);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting a TypeScript array into a generic array of a specific class

I am attempting to convert a JSON source array with all string values into another array of typed objects, but I keep encountering errors. How can I correct this code properly? Thank you. Error 1: There is an issue with converting type '({ Id: string ...

"Error message: 'ng' command cannot be found even after completing the installation of Node

Recently, I set up a fresh MacOS Catalina operating system on my computer. When I attempted to run my old project using 'ng serve', I encountered the error message 'ng: command not found'. To troubleshoot, I installed npm 6.12.1 via nod ...

The function `Object.entries().map()` in TypeScript does not retain the original data types. What changes can I make to my interface to ensure it works correctly, or is there a workaround

Working with this interface: export interface NPMPackage { name: string; description: string; 'dist-tags': { [tag: string]: string; }; versions: { [version: string]: { name: string; version: string; dependencie ...

Adjusting Size Dynamically for Tooltips Based on Content Length

In my angular app using ng-bootstrap v4.2.1 on an older codebase, I have successfully created a tooltip with some very long sentences as content. I am trying to make the tooltip 800px wide, but when I set the width, only the first few words fill the space. ...

The function userRole consistently returns "user" regardless of the role being admin

I am facing an issue with the getTeamMembers() method while trying to identify which members are admins in a private team. Even though I am logged in as an admin, the userRole value always shows as "user". Can anyone assist me with this problem? import { ...

Contact the help desk and receive information that is currently unknown

There are a few issues that I'm struggling to resolve. I am utilizing SwaggerService to fetch data, but the response is coming back as undefined. import {SwaggerService} from '../../services/swagger.service'; export class TestComponent im ...

Utilizing Angular Forms for dynamic string validation with the *ngIf directive

I have a challenge where I need to hide forms in a list if they are empty. These forms contain string values. I attempted to use *ngIf but unfortunately, it did not work as expected and empty fields are still visible in the list. How can I address this iss ...

Sending data between components in Angular can be achieved by using various methods. One common approach is to utilize

I am encountering an issue with a component named customers.component Below is the code from the customers.component.ts file: @Component({ selector: 'app-customer', templateUrl: './customer.component.html', styleUrls: ['./cu ...

Utilize TypeScript in creating a Chrome extension to fetch elements by their ID

I'm currently working on a Chrome extension using Angular and Typescript, and I have encountered an issue with accessing the document element by its id from the active tab. While this works perfectly fine in JavaScript, I am facing difficulties in ach ...

Tips for utilizing [ngClass] with various class situations in Angular4

My current div structure is as follows: <div class="class-a" [ngClass]="{'class-b': !person.something}"> However, I now need to add an additional condition... I want the div to have class-a if one condition is met, class-b if another con ...

Exploring the integration of SendGrid with Angular and NodeJS

Struggling to send an email using Angular and SendGrid. The NPM package is installed correctly, but I'm facing challenges with code implementation. Generated API key stored in directory: echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > se ...

Leveraging Java and TypeScript code for specific functionalities within an Ionic 2 Android application

When it comes to creating hybrid apps that support Windows, iOS, and Android simultaneously using web technologies such as Html, CSS, and Js, Ionic is the go-to choice. However, there may be certain features not supported by the Ionic 2 framework. Is it ...

Angular 2: Utilizing Http Subscribe Method with "this" Context Pointer

Question: http.request('js/app/config/config.json').subscribe(data => { this.url = data.json().url; }); It seems that "this" is pointing to Subscriber instead of the parent class. I was under the impression that the fat- ...

Exploring Angular12: Enhancing GraphQL Communication by Intercepting HTTP Requests and Responses

Currently, we have a Graphql response handler implementation. export const handleResponse = (operation: DocumentNode): OperatorFunction<any, any> => { return pipe( map((response: any) => { const definition = operation.definitions[0] ...

Utilizing GraphQL subscriptions to dynamically update objects in response to specific events

My challenge involves updating the users' password that I retrieved previously when an event is published to the PasswordUpdated Channel. Here's my current approach: this.apollo.subscribe({ query: this.passwordUpdatedSubscription }).subscribe( ...

What is the best way to refresh a Load on Demand feature in Nativescript?

In my RadListView, the data displayed changes depending on the day selected by the user in a calendar. Currently, I am using loadOnDemandMode = "Manual" and it functions smoothly until all the available data is loaded. At that point, I trigger listView.no ...

Use ng2-dragula to organize elements within a div container

During my exploration of implementing drag and drop functionality, I stumbled upon the ng2-dragula project. I found myself wondering how to group the following structure into a single 'drake' entity: <div> <img .../> <img .. ...

Is it possible to iterate through http.post multiple times with a different URL each time?

Struggling to find the right approach to repetitively send an Angular2(4) http.post request a specified(X) number of times. Illustrating with an example, here is the http.post code: this.http.post(`http://url.com/param/name01`, dbJson, options) My goal ...

What is the proper way to specify the type for the `clean-element` higher-order-component in React?

Error message: 'typeof TextareaAutosize' argument cannot be assigned to a type 'Component<{}, {}, any>'. Error: Property 'setState' is not found in 'typeof TextareaAutosize'. Here is the code snippet causin ...

transmit information to a service using Angular 8

There are 4 multiselect dropdowns, and when I click on an event, I save the data array of objects in the same component. Now, I need to send this data to display it in another component. To achieve this, I am using a service. However, every time I send th ...