2017-10-24 6 views
0

ionicで初めてキャッシュを設定しています。ここでコードである:promiseが完了する前に実行されているPromise.allの()関数 - Ionic/Angular

listProducts(): Promise <any> { 
    let cacheKey = 'products'; 
    this.cache.removeItem(cacheKey); 
    let promises_array:Array<any> = []; 
    let results; 
    return new Promise((resolve, reject) => { 
     this.cache.getItem(cacheKey).catch(() => { 
      this.list = this.af.list('/products'); 
      this.subscription5 = this.list.subscribe(items => { 
       for (let x = 0; x < items.length; x++) { 
        promises_array.push(new Promise((resolve, reject) => { 
         console.log(JSON.stringify(items[x].customMetadata) + ": this is the customdata (((()()()()()"); 
         let storageRef = firebase.storage().ref().child('/settings/' + items[x].customMetadata.username + '/profilepicture.png'); 
         storageRef.getDownloadURL().then(url => { 
          console.log(url + "in download url !!!!!!!!!!!!!!!!!!!!!!!!"); 
          items[x].customMetadata.profilepic = url; 
         }).catch((e) => { 
          console.log("in caught url !!!!!!!$$$$$$$!!"); 
          items[x].customMetadata.profilepic = 'assets/blankprof.png'; 
         }); 
         //this.startAtKey = item.$key; 
         this.productListArray.push(items[x].customMetadata); 
        })); 
       }; 
      }) 
      results = Promise.all(promises_array); 
      results.then(() => { 
       //setTimeout(() => { 
       console.log(JSON.stringify(this.productListArray) + " value value vlaue productlistarray"); 
       this.productListArray.reverse(); 
       return this.cache.saveItem(cacheKey, this.productListArray); 
       //}, 3000); 
      }) 
     }).then(data => { 
      console.log("Saved data: ", data); 
      resolve(); 
     }) 
    }) 
} 

基本的に線console.log(JSON.stringify(this.productListArray) + " value value vlaue productlistarray");は、空の配列を印刷 - []。すべての約束が完了する前にコードが実行されるべきではないので、この変数にデータがあるはずです。 setTimeoutがコメントアウトされていることに気づくでしょう - 私がコメントしたとき、それはうまくいきます - しかし、これは明らかに許容可能な解決策ではありません。どんな助けも素晴らしいだろう。

答えて

0

は、私はこの問題は

console.log(JSON.stringify(this.productListArray) + " value... 

が実行されたときに基本的に、リストがまだフェッチされ、このライン

this.list.subscribe(items => { 

にあると思います。だから私はpromises_arrayが空であると思う(あなたはそれをチェックすることができる)。

あなたがマッピングlistによってこれを整理することができ、個々の約束をプッシュするのではなく、約束の配列に(マップのどちらか()またはflatMap()、これをチェックする必要があります)。

を私は(これはマップだと思いますまたはflatMap)コードが必要ですが、テストが必要です。インデントレベルごとに多くのリターンが必要であることに注意してください。

const promises_array = this.list.map(items => { 
    return items.map(item => { 
    return new Promise((resolve, reject) => { 
     console.log(JSON.stringify(items[x].customMetadata) + ": this is the customdata (((()()()()()"); 
     let storageRef = firebase.storage().ref().child('/settings/' + items[x].customMetadata.username + '/profilepicture.png'); 
     return storageRef.getDownloadURL().then(url => { 
     console.log(url + "in download url !!!!!!!!!!!!!!!!!!!!!!!!"); 
     items[x].customMetadata.profilepic = url; 
     }).catch((e) => { 
     console.log("in caught url !!!!!!!$$$$$$$!!"); 
     items[x].customMetadata.profilepic = 'assets/blankprof.png'; 
     }); 
     this.productListArray.push(items[x].customMetadata); 
    }));  
    }) 
} 
Promise.all(promises_array) 
    .then(() => { 
    console.log(JSON.stringify(this.productListArray) + " value value vlaue productlistarray"); 
    this.productListArray.reverse(); 
    this.cache.saveItem(cacheKey, this.productListArray); 
}) 
+0

私のアプリではこのテクニックを実際に使っています...悪いことをやってみてください – ewizard

関連する問題