Reactjs 在useFirestoreConnect钩子中使用new Date(),其中查询导致连续文档读取
我正在成功查询时间戳早于当前日期的Firestore文档,如下所示:Reactjs 在useFirestoreConnect钩子中使用new Date(),其中查询导致连续文档读取,reactjs,google-cloud-firestore,react-redux,react-redux-firebase,redux-firestore,Reactjs,Google Cloud Firestore,React Redux,React Redux Firebase,Redux Firestore,我正在成功查询时间戳早于当前日期的Firestore文档,如下所示: import React from 'react' import CircularProgress from '@material-ui/core/CircularProgress' import { useSelector } from 'react-redux' import { useFirestoreConnect } from 'react-redux-firebase' import Grid from '@mat
import React from 'react'
import CircularProgress from '@material-ui/core/CircularProgress'
import { useSelector } from 'react-redux'
import { useFirestoreConnect } from 'react-redux-firebase'
import Grid from '@material-ui/core/Grid'
function MeetingHistory({ userId }) {
const todaysDate = new Date()
useFirestoreConnect([
{ collection: 'users',
doc: userId,
subcollections: [{ collection: 'scheduled',
where: [
['date', '<', todaysDate],
]
}],
storeAs: `${userId}-history-scheduled`
}
])
const pastMeetings = useSelector(state => state.firestore.ordered[`${userId}-history-scheduled`])
if (!pastMeetings) return (
<Grid container direction='row' alignItems='center' justify='center'>
<CircularProgress />
</Grid>
)
console.log('meetings', pastMeetings)
return (
<div>
<p>I am going to render past meeting data here</p>
</div>
)
}
export default MeetingHistory
从“React”导入React
从“@material ui/core/CircularProgress”导入CircularProgress
从“react redux”导入{useSelector}
从“react-redux firebase”导入{useFirestoreConnect}
从“@material ui/core/Grid”导入网格
函数会议历史({userId}){
const todaysDate=新日期()
使用FireStoreConnect([
{集合:'用户',
doc:userId,
子集合:[{collection:'scheduled',
其中:[
['date','我怀疑是否有人好奇,但如果他们在这里,这就是有效的方法。使用常规Firestore查询:
const todaysDate = new Date()
const [pastMeetings, setPastMeetings] = useState(null)
useEffect(() => {
const db = firebase.firestore()
db.collection('users').doc(userId).collection('scheduled').where('date', '<=', todaysDate).limit(15)
.get()
.then(snap => {
var temp = []
snap.forEach(doc => {
temp.push(doc.data())
})
setPastMeetings(temp)
})
.catch(err => {
console.log('error', err)
})
}, [userId])
if (!pastMeetings) return (
<Grid container direction='row' alignItems='center' justify='center'>
<CircularProgress />
</Grid>
)
console.log('meetings', pastMeetings)
const todaysDate=新日期()
const[pastMeetings,setPastMeetings]=useState(null)
useffect(()=>{
const db=firebase.firestore()
db.collection('users').doc(userId).collection('scheduled')。其中(“date”,“您不能将变量值传递给查询。日期随时间变化的事实不会导致查询根据该变化的值更改其结果。一旦向查询提供值,该值从查询的角度来看将永远不会更改。您必须发出另一个查询才能将日期传递给查询nge。我从未听说过不能在查询中使用变量。即使在redux firebase文档中,它们也显示了一个在查询中使用变量的示例。当我使用new Date()时,我的查询会不断执行。如果我使用new Date('在此处硬编码日期')然后它只按预期执行一次。我发布这个问题的原因是我认为它不正常,但当我使用new Date()时,查询肯定会连续执行您可以传递变量的内容,但其值在查询过程中不能更改。它接受传递时的值。如果变量稍后更改值,则对查询没有影响。如果需要新结果,则必须使用新值重复查询。是否确实在重复此查询?可能会添加一些调试日志ng?我们需要更多的上下文-此代码位于何处?我们不知道如何调用它。它是在循环中调用的吗?在组件中调用的吗?给出的示例非常不完整。您是否尝试通过编程方式指示当前时间var todaysDate=Date.now();
而不是使用Date()
创建日期对象。。。