Firebase Firestore 时间戳查询未返回文档。

3

我试图从一个收集中检索一些文档,这些文档的时间戳落在特定的一天。我知道我的收集中有落在这些时间戳之间的文档,但它始终返回空值。如果我删除“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]);


1
我们无法查看您数据库中的数据,因此无法确定该查询是否会返回任何内容。我们需要查看查询和数据库的原始数据。另外,我不确定您所说的“doc.data()从未未定义”是什么意思。您是说它总是被定义吗?这似乎是您想要的。 - Doug Stevenson
我认为“doc.data()从未未定义”是从解释QuerySnapshot中的DocumentSnapshot永远不会是undefined文档复制而来。 - Dharmaraj
你能确认一下你要获取的文档中是否包含startTime字段,并且它是一个数字吗? - Peter Obiechina
感谢您抽出时间来查看这个问题。我已经添加了Firestore数据库的屏幕截图。 - Olly
1个回答

2
尝试使用Firestore Timestamp对象代替在where()中使用时间戳,因为您将其存储为时间戳。此外,该值存储在字段startTime中,因此您不需要在where()中使用.seconds
firestore
  .collection('bookings')
  .where('studio.ID', '==', 'kM8p1jSenI4M0Mr1PzBo')
  .where('startTime', '>=', firebase.firestore.Timestamp.fromMillis(dayjs(date).valueOf())) 
  .where('startTime', '<', firebase.firestore.Timestamp.fromMillis(dayjs(date).add(1, 'day').valueOf())) 
  .get()

如果您需要在查询中使用Unix时间戳,则必须将其存储为Firestore中的数字


尝试过了,但没有成功。结果仍然相同。我已经将完整的代码片段添加到问题中。 - Olly
@Olly,你可以尝试使用firebase.firestore.Timestamp.fromMillis()代替new Date()吗? - Dharmaraj
很遗憾仍然没有数据。能够正确登出 Firestore 时间戳,但是却没有数据返回。 - Olly
@Olly,为什么你写了“.where('startTime.seconds')”?应该只写“.where('startTime')”。 - Dharmaraj
是的!解决了,我刚刚注意到这一点,当我试图尝试各种方法让它工作时...感谢您提供的解决方案! - Olly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接