排序Firebase存储数据(Vue)

时间:2020-08-24 13:49:51

标签: javascript vue.js vuejs2 vuex firebase-storage

我正在从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中的数据进行排序?

1 个答案:

答案 0 :(得分:1)

在提交之前简单地添加一个排序:

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)
      })
    },