1

I'm upgrading a filter from a static object to an object retrieved from our database, but I can't seem to get a proper return. This filter takes in an integer which represents a named location, it looks up the location with the key, and returns the name. After troubleshooting, I'm getting close as I can see the object from the database and I can see some of the lookups correctly inside of the then section, but it's not being returned at the end of the filter. Is there a better method on getting this with a filter?

stPartFieldFilters.js

angular.module('app').filter('partLocation', function(stPartMgmtSvc, $q) {
  var locs;
  function getLocations() {
    if(!locs) {
      var dfd = $q.defer();
      stPartMgmtSvc.getLocations().then(function(res) { locs = res; dfd.resolve(locs); }, function(response) { dfd.reject(response.data.reason) });
      return dfd.promise;
    }
    else {
      var dfd = $q.defer();
      dfd.resolve(locs);
      return dfd.promise;
    }
  }

  function getResults(loc, type) {
    var lr = null; // this should be updated once a match is found below
    getLocations().then(function(ls) {
      if (loc || loc === 0) {
        loc = loc.split(',');
        if(typeof loc === 'object') {
          var res = new Array;
          loc.forEach(function(l) {
            res.push(ls[l].name)
          });
          if(!type) { lr = res.toString().replace(',', '<br>'); } // this line provides the correct output; see below (LOCFLTRRES1)
          else { lr = res.toString().replace(',', ', '); }
        }
        else { lr = ls[loc].name; }
      }
      else { lr = false; }; 
    });

    return lr; // return the updated result
  }

  return function(loc, type) {
    return getResults(loc, type); //return the final result for the filter
  }
});

inventory.jade

~~~
td.desktop-only {{part.partLoc | partLocation:1}}
~~~

Here is an image of the results of the database object. Database object

Here is an image of the result inside of the filter. This should be set to lr and returned to the main filter function. Map result

Here is the expected result.

Expected result

Finally, here is the actual result.

Actual result

jlindsey
  • 43
  • 1
  • 7
  • A filter needs to be synchronous. You can't pass a promise to the view – charlietfl Apr 06 '21 at 19:22
  • Does this answer your question? [AngularJS : Asynchronously initialize filter](https://stackoverflow.com/questions/19046641/angularjs-asynchronously-initialize-filter) – Julien Apr 07 '21 at 10:02

0 Answers0