角度承诺未正确解析



我正在努力获得在Firebase查询后解决的承诺。 本质上,我想从一个表中获取所有指定的键,然后遍历另一个表以获得我想要的艺术品。

艺术家工厂.js

'use strict';
angular.module('artvoicesApp')
  .factory('Artist', function(localStorageService, FIREBASE_URL, $q) {
    var artistData = {};
    var userKey = localStorageService.get('userKey');
    var accountKey = localStorageService.get('accountKey');
    var artistRef = FIREBASE_URL.child('v2/artist');
    var accountRef = FIREBASE_URL.child('v2/account/' + accountKey);
    var userRef = FIREBASE_URL.child('v2/user/' + userKey);
    artistData.addArtist = function(artistName) {
      var artist = artistRef.push();
      accountRef.child('artists/' + artist.key()).set(true);
      userRef.child('artists/' + artist.key()).set(true);
      artist.set({name: artistName});
      artist.child('users/' + userKey).set(true);
      artist.child('accounts/' + accountKey).set(true);
    };
    artistData.getArtistKeys = function() {
      var artistKeys = [];
      var defer = $q.defer();
      accountRef.child('artists').once('value', function(snapshot) {
        snapshot.forEach(function(childSnapShot) {
          artistKeys.push(childSnapShot.key());
        });
        defer.resolve(artistKeys);
      });
      return defer.promise;
    };
    artistData.getArtists = function(artistKeys) {
      var artistObj = {};
      var artistRef = FIREBASE_URL.child('v2/artist');
      var defer = $q.defer();
      artistKeys.forEach(function(artist) {
        artistRef.child(artist).once('value', function(snapshot) {
          artistObj[artist] = snapshot.val();
        });
        defer.resolve(artistObj);
      });
      return defer.promise;
    };
    return artistData;
  });

艺术品.控制器.js

Artist.getArtistKeys().then(function(artistKeys) {
  Artist.getArtists(artistKeys).then(function(artists) {
      vm.artists = artists;
      console.log(vm.artists);
  });
});

如果我将 vm.art 设置为超时,它会返回相应的数据。

这是你的问题:

  artistKeys.forEach(function(artist) {
    artistRef.child(artist).once('value', function(snapshot) {
      artistObj[artist] = snapshot.val(); // <<== This is called second, at an unspecified time in the future
    });
    defer.resolve(artistObj);  // <<== This is called first
  });

artistObj的所有分配都将在您调用defer.resolve(artistObj)后的某个时间发生。这就是为什么一旦您添加超时,它似乎就可以工作的原因。

您需要将您的艺术家收藏映射到承诺集合,然后等待所有这些承诺得到解决。

artistData.getArtistKeys = function(artistKeys) {
  var artistObj = {};
  var artistRef = FIREBASE_URL.child('v2/artist');
  var allPromises = artistKeys.map(function(artist) {
    var childDefer = $q.defer();
    artistRef.child(artist).once('value', function(snapshot) {
      artistObj[artist] = snapshot.val();
      childDefer.resolve();
    });
    return childDefer.promise();
  });
  // allPromises is now an array of promises
  var defer = $q.defer();
  $q.all(allPromises).then(function() {
    defer.resolve(artistObj);
  });
  return defer.promise();
}

相关内容

  • 没有找到相关文章

最新更新