【问题标题】:array value is not displaying数组值未显示
【发布时间】:2017-12-06 17:56:49
【问题描述】:

我有一个功能可以帮助显示图像缩略图 我最初以这种格式调用变量

(function(){
var catalog = {

thumbnails: [
  'https://farm6.staticflickr.com/5576/18670011368_b25a92d37e_k.jpg',
  'https://unsplash.imgix.net/photo-1428930377079-45a584b6dd6b?fit=crop&fm=jpg&q=75&w=1050',
  'https://unsplash.imgix.net/photo-1428976365951-b70e0fa5c551?fit=crop&fm=jpg&h=700&q=75&w=1050',
  'https://ununsplash.imgix.net/photo-1422207134147-65fb81f59e38?fit=crop&fm=jpg&h=800&q=75&w=1050',
]
.
.
.
catalog.init();

我需要用 firebase 中的 imageUrls 替换缩略图,所以我创建了一个空数组和一个函数,用于获取一些变量和 imageurls 的快照值

var thumbnails = [];

function showproductpage(element){


var itemdetailref = Cataloguedatabase.ref('/Listings/'+ listingID);

return itemdetailref.once('value').then(function(snapshot){
    var results=snapshot.val();
 var mainimage = results.ProductImageUrl;
 var thumbnail1 = results.ProductImage1Url;
 var thumbnail2 = results.ProductImage2Url;

  thumbnails.push(mainimage,thumbnail1,thumbnail2);

}

所以我更新的缩略图功能看起来像这样

(function(){
var catalog = {

thumbnails ,
....

catalog.init();
})()

但是当我运行这个时,没有图像出现缩略图,控制台上也没有显示错误。我做错了什么,我该如何解决?

【问题讨论】:

  • 你在哪里执行showproductpage
  • @JaromandaX 在调用缩略图函数之前执行
  • 但它是异步的

标签: javascript arrays firebase firebase-realtime-database


【解决方案1】:

在加载图像之前调用自调用函数。尝试在自调用功能中替换图像。

编辑:

如 cmets 中所述,函数的异步特性也很重要。调用函数时,程序的执行将继续,因此您的数组为空。我建议你使用Promise 对象,然后用你的thumbnails 链接其他操作。

【讨论】:

  • showproductpage 的异步特性也很重要
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
  • 2013-05-28
  • 1970-01-01
  • 2018-09-23
相关资源
最近更新 更多