How can you retrieve multiple values with momentjs from firebase?

我是研究僧i 提交于 2020-01-16 19:31:52

问题


I get all data from firebase and store it on array and from that array i take all posted_at data (time when was something posted). My goal is to retrieve all that data in some sort of time format and problem is that it won't retrieve multiple values. I am using Vuejs.

<template>
  <div v-for="data in array">
    {{ time }}
  </div>
</template>

<script>
import moment from 'moment'
export default{
  data(){
    return{
      array:[]//example:1577200868199, 1577200868189,...
    }
  },
  computed:{
    time(){
      moment(this.array.posted_at).format('MMMM Do YYYY, h:mm:ss a')
    }
  }
}
</script>

P.S. I have tried using a for and a while loop but it's not working


回答1:


This is a nice case to use Vue filter

<template>
  <div v-for="data in array">
    {{ data | formatTime }}
  </div>
</template>

<script>
import moment from 'moment'
export default{
  data(){
    return{
      array: []//example:1577200868199, 1577200868189,...
    }
  },
  filters: {
    formatTime: function (value) {
      return moment(value).format('MMMM Do YYYY, h:mm:ss a')
    }
  }
}
</script>



回答2:


With moment you can only parse one item at a time, so you can change your code to loop over your array and parse each item individually. It would look something like this.

this.array.forReach(posted_at => {
 moment(posted_at).format('MMMM Do YYYY, h:mm:ss a')
})


来源:https://stackoverflow.com/questions/59494301/how-can-you-retrieve-multiple-values-with-momentjs-from-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!