You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firestore读取指定聊天消息正常,但无法读取聊天列表的问题求助

Firestore读取指定聊天消息正常,但无法读取聊天列表的问题求助

各位大佬,我现在碰到一个Firestore的棘手问题,想请教下大家:

我能正常读取某条具体聊天里的消息(getMessages函数可以正常返回消息数据),但当我尝试读取该酒店下的所有聊天列表时(getChats函数),控制台打印出来的却是空数组,完全读不到任何聊天数据。

以下是我的代码:

import {
  collection,
  addDoc,
  serverTimestamp,
  query,
  orderBy,
  onSnapshot,
  getDocs,
} from "firebase/firestore";
import { db } from "./firebase";

export const getMessages = () => {
  const messagesRef = collection(
    db,
    "envs/PROD/hotels/Hotel_001/chats/Chat_001/messages" // 这个路径能正常工作
  );
  const q = query(messagesRef);
  return onSnapshot(q, (snapshot: any) => {
    const messages = snapshot.docs.map((doc: any) => ({
      id: doc.id,
      ...doc.data(),
    }));
    console.log(messages); // 输出正常:[{text: 'hi'}, {text: 'great'}...]
  });
};

export const getChats = () => {
  const messagesRef = collection(
    db,
    "envs/PROD/hotels/Hotel_001/chats" // 这个路径无法读取到数据
  );
  const q = query(messagesRef);
  return onSnapshot(q, (snapshot: any) => {
    const messages = snapshot.docs.map((doc: any) => ({
      id: doc.id,
      ...doc.data(),
    }));
    console.log(messages); // 输出空数组:[]
  });
};

我已经核对过路径拼写,看起来是没问题的,而且Chat_001确实是chats集合下存在的文档。有没有大佬能帮我分析下可能的原因?比如安全规则的限制?或者我调用Firestore API的方式有哪里疏漏?

备注:内容来源于stack exchange,提问作者Aagam Jain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 20:18:00