我试图从一个收集中检索一些文档,这些文档的时间戳落在特定的一天。我知道我的收集中有落在这些时间戳之间的文档,但它始终返回空值。如果我删除“startTime”查询参数,则可以检索到文档。
我是否错误地查询了时间戳?
Firestore中的StartTime以时间戳形式存储。
// Get bookings from firestore
firestore
.collection('bookings')
.where('studio.ID', '==', 'kM8p1jSenI4M0Mr1PzBo') /// Works fine with this query
.where('startTime', '>=', dayjs(date).valueOf()) /// 1631415616880
.where('startTime', '<', dayjs(date).add(1, 'day').valueOf()) /// 1631502016880
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.table(doc.data());
setBookings(doc.data());
});
还尝试将查询作为日期对象:
const [date, setDate] = useState(new Date(new Date().setHours(0, 0, 0, 0))); //setting initial state to current day midnight
const [step, setStep] = useState(0); // Count the days ahead the user is trying to book.
const [alert, setAlert] = useState(false); // Alert when trying to exceed booking window.
const [bookings, setBookings] = useState({});
const bookingWindowAllowed = 7; // Used to limit the forward bookings (evaluated vs state.step)
useEffect(() => {
setLoading(true);
console.log('Set date is:', date);
const setDate = new Date(dayjs(date).valueOf()); //Date object for today
const futureDate = new Date(dayjs(date).add(1, 'day').valueOf()); //Date object for tomorrow
console.log('Set date is:', setDate);
console.log('Future date is:', futureDate);
// Get bookings from firestore
firestore
.collection('bookings')
.where('studio.ID', '==', 'kM8p1jSenI4M0Mr1PzBo') /// Works fine with this query
.where('startTime', '>=', setDate) /// 1631415616880
.where('startTime', '<', futureDate) /// 1631502016880
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.table(doc.data());
setBookings(doc.data());
});
setLoading(false);
})
.catch((error) => {
console.log('Error getting documents: ', error);
setLoading(false);
});
}, [date]);
QuerySnapshot
中的DocumentSnapshot
永远不会是undefined
的文档复制而来。 - DharmarajstartTime
字段,并且它是一个数字吗? - Peter Obiechina