angulartypescriptjsdata

js-data dataStore.find() can't find cached results


I am calling dataStore.find() with the same parameters each time. The current adapter is the HttpAdapter. However, after the initial call, it does not seem to be able to find the data in the cache and makes a call to the API again. This is happening even though the data is in fact being cached, js-data just can't seem to find it:

same record added each time


Solution

  • The data I was getting back from the API did not contain an id property. When js-data looks through the cache after a call to dataStore.find(), it looks for an item with a matching idAttribute, which is id by default. Thus, every time the data was requested, it was being added to the cache but could not be found afterwords. To fix this, I added the appropriate idAttribute to my mapper:

    this.mapper = this.dataStore.defineMapper('community', {
      schema: this.communitySchema,
      endpoint: 'cm',
    
      // We don't have an 'id' property so the cached records won't be found.
      // Thus, we set the 'idAttribute' to 'code', which is the unique identifier
      // for communities.
      idAttribute: 'code'
    });