Polymerのdom-repeatを使用してイメージを表示する際に問題があります。 Firebase Storageを使って画像を保存します。計算バインディングによるPolymerのdom-repeatを使用したFirebaseからのファイルURLの動的フェッチ
<firebase-query id="productsQuery"
data="{{products}}"
limit-to-first="2"></firebase-query>
上記の要素のパスは動的に更新され、正常に動作します。
<template is="dom-repeat" items="[[products]]" index-as="index">
<paper-card image="[[computePhotoURL(item.$key)]]" heading="[[item.name]]">
<div class="card-content">
<p class="shop-name">[[shopName]]</p>
<small>Php [[item.price]]</small>
</div>
</paper-card>
</template>
上記の要素はすべて正常に動作していますが、情報は表示されますが、計算された関数では正しく返されません。
ここで計算された結合コードです:
computePhotoURL: function(key) {
var photo;
firebase.storage()
.ref('users/' + this.data[0].$key + '/products/' + key)
.getDownloadURL()
.then(function(url) {
photo = url;
return url;
}.bind(this)).catch(function(error) {
console.log('there was an error');
});
return photo;
}
はそれがfirebaseストレージから正しいURLが表示されますが、ペーパー - にバインドさ適切な値を返すようには見えません上記の関数からURLをログに記録することによりカードの画像属性。
アイデア?事前にありがとう:)
私は
computePhotoURL: function(key) {
var photo;
firebase.storage()
.ref('users/' + this.data[0].$key + '/products/' + key)
.getDownloadURL()
.then(function(url) {
photo = url;
console.log(photo);
//logs correct url
}.bind(this)).catch(function(error) {
console.log('there was an error');
});
console.log('photo', photo);
//logs undefined
return photo;
}
にそれを変更しようと、それが正しいURLの前に、未定義の最初を記録します。そのため、写真変数は変更される前に返されました。これを修正する方法のヘルプ?ずっとこのJSとの良好ではない:(
あなたは "return url;"バインドが完了する前にだから、決して「写真を返す」ことはありません。 – zerohero
私は 'return url;'を削除しようとしました。また、 'then function'の外に写真を記録することもできません:(@zerohero –
show .getDownloadURL()function – zerohero