javascript - Firebase firestore 时间戳查询不返回任何文档

标签 javascript firebase google-cloud-firestore

我正在尝试根据特定日期的时间戳从集合中检索一些文档。我知道我的收藏中有文件在这些时间戳之间,但它一直没有返回任何内容。如果我删除“startTime”查询参数,那么我就能够检索文档。

我是否错误地查询了时间戳?

StartTime 在 firestore 中存储为时间戳

// 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());
        });

Database

还尝试将查询作为日期对象:

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]);

最佳答案

尝试使用 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 时间戳,则必须将其作为 number 存储在 Firestore 中。

关于javascript - Firebase firestore 时间戳查询不返回任何文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69139734/

相关文章:

php - 将变量格式化为 map 上信息窗口中的链接

javascript - 如何在 if 条件下增加 for-in 循环中 jest 的分支覆盖率?

javascript - Casperjs随机延迟

firebase - 如何在 Google Firebase 中区分 Android/IOS 的暂存/开发和生产构建以进行分析和崩溃

javascript - 动态加载 firebase 模块? (v9)

javascript - 识别并替换 html 字符串中的格式化日期范围

javascript - OnDelete Firebase 触发器不触发

firebase - 在Flutter Cloud Firestore中, 'DateTime'类型不是 'String'类型的子类型

firebase - 如何修复 "firestore: quota exceeded error while uploading rules"

java - 有没有办法将 firestore 中的数据值存储到数组中?