首页 > 解决方案 > 对 Firebase 存储数据进行排序 (Vue)

问题描述

我正在从我的 firebase 存储中检索一些图像(仅限 URL)。

这很好用

    async loadStoragePictures(){
      fb.storage.ref('images/products').listAll().then(snap => {
        const imageUrlArray: any = []

        snap.items.forEach(itemRef => {
          itemRef.getDownloadURL().then(imgUrl => {
            imageUrlArray.push(imgUrl)
          })
        })

        store.commit('setPictures', imageUrlArray)
      })
    },

不幸的是,我想按上次更改日期对这些 URL 进行排序。

添加.sort(by: { $0.name > $1.name })(或等效的lastAction)我在快速帮助中看到的方式不起作用。

那么我怎样才能让我的数据snap.items得到排序呢?

标签: javascriptvue.jsvuejs2vuexfirebase-storage

解决方案


只需在提交之前添加一个排序:

async loadStoragePictures(){
      fb.storage.ref('images/products').listAll().then(snap => {
        const imageUrlArray: any = []
        const myItems = snap.items.sort((a, b) => {
          return a.name.localeCompare(b.name);
        });
        myItems.forEach(itemRef => {
          itemRef.getDownloadURL().then(imgUrl => {
            imageUrlArray.push(imgUrl)
          })
        })

        store.commit('setPictures', imageUrlArray)
      })
    },

推荐阅读