我正在从Firebase提取数据并尝试对其进行过滤,以便我可以根据需要以不同的方式显示数据,数据已正确提取并到达,但我正努力在数据到达时进行过滤,在数据之前触发过滤器似乎到了。
承诺:
eventsToday() {
let uid = this.$store.state.user.activeUser
let today = '2034'
const events = new Promise((resolve, reject) => {
let fetchedData = getCalanderList(uid)
resolve(fetchedData)
})
events.then(data => {
console.log('Recieved data:', data.data)
// Returns Array with 5 Objects
let filteredData = data.data.filter(item => {
return item.dateStamp == today
})
console.log('Filtered Data:', filteredData)
// Returns Empty Array
})
异步/等待:
async eventsToday() {
let uid = this.$store.state.user.activeUser
let today = '2034'
const events = await getCalanderList(uid)
console.log('Recieved Data', events.data)
// Returns Array with 5 Objects
const filteredData = await events.data.filter(item => {
return item.dateStamp == today
})
console.log('Filtered Data:', filteredData)
// Returns Empty Array
},
events.data返回包含5个对象的数组:
[{
日:“星期一”,
dateStamp:“ 2033”
},
{
日:“星期二”,
dateStamp:“ 2034”
},
{
日:“星期三”,
dateStamp:“ 2034”
},
{
日:“星期四”,
dateStamp:“ 2035”
},
{
日:“星期五”,
dateStamp:“ 2035”
}]
答案 0 :(得分:0)
很有可能您的日期相等性检查失败,您不应该像过滤函数中那样比较日期,您应该首先将它们解析为Date
对象,然后使用它们的getTime
方法进行比较,如下所示:
date1.getTime() === date2.getTime()