Skip to content

FindAll with object as a result. #113

Closed
@kenjiqq

Description

@kenjiqq

When using findAll and the resource returns a single object as a result, not an array. Some APIs could work like that if you have filtered or something causing only one answer to be returned from the server instead of returning an array with one element. But this code could fail when that happens because the angular.forEach starts to iterate over the values in the object. And if a value is null the code crashes.

function processResults(utils, data, resourceName, queryHash) {
  var resource = this.store[resourceName],
    idAttribute = this.definitions[resourceName].idAttribute,
    date = new Date().getTime();

  data = data || [];

  // Query is no longer pending
  delete resource.pendingQueries[queryHash];
  resource.completedQueries[queryHash] = date;

  // Update modified timestamp of collection
  resource.collectionModified = utils.updateTimestamp(resource.collectionModified);

  // Merge the new values into the cache
  var injected = this.inject(resourceName, data);

  // Make sure each object is added to completedQueries
  angular.forEach(injected, function (item) {
    resource.completedQueries[item[idAttribute]] = date;
  });

  return injected;
}

There should probably be a check to only run the loop if injected is an array, and just run the code on the one object if it is not.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions