【问题标题】:What is the version 9 equivalent of this in firebase?Firebase 中与此等效的版本 9 是什么?
【发布时间】:2022-01-28 05:44:31
【问题描述】:

我正在尝试将我的版本 8 工作代码转换为版本 9,但我无法弄清楚为什么我收到错误:'FirebaseError: Expected type 'Query',但它是:自定义 DocumentReference 对象'。它似乎不像 messagesRes,但我输入了一个查询,我认为我正确地获取了子集合。

version 8: working
 export async function getServerSideProps(context) {
   const chatRef = db.collection('chats').doc(context.query.id);

   const messagesRes = await chatRef
     .collection('messages')
     .orderBy('timestamp', 'asc')
     .get();

   const messages = messagesRes.docs
    .map(doc => ({
       id: doc.id,
       ...doc.data(),
    }))
       .map(messages => ({
       ...messages,
       timestamp: messages.timestamp.toDate().getTime(),
     }));

   const chatRes = await chatRef.get();

   const chat = {
     id: chatRes.id,
     ...chatRes.data(),
   };

   return {
     props: {
       messages: JSON.stringify(messages),
       chat,
     },
   };
 }
version 9: 
export async function getServerSideProps(context) {
  const chatRef = doc(db, 'chats', context.query.id);

  const messagesRes = await getDocs(
    query(collection(chatRef, 'messages'), orderBy('timestamp', 'asc'))
  );

  const messages = messagesRes.docs
    .map(doc => ({
      id: doc.id,
      ...doc.data(),
    }))
    .map(messages => ({
      ...messages,
      timestamp: messages.timestamp.toDate().getTime(),
    }));

  const chatRes = await getDocs(chatRef);

  const chat = {
    id: chatRes.id,
    ...chatRes.data(),
  };

  return {
    props: {
      messages: JSON.stringify(messages),
      chat,
    },
  };
}

【问题讨论】:

    标签: javascript node.js reactjs firebase google-cloud-firestore


    【解决方案1】:

    根据API documentation

    export declare function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
    

    在你的代码中你正在做:

    const chatRes = await getDocs(chatRef);
    

    这就是您看到错误的原因:

    'FirebaseError:预期类型'Query',但它是:自定义 DocumentReference 对象'。

    因为您传递的是文档参考,而不是查询

    就像你在这里所做的那样:

    const messagesRes = await getDocs(
        query(collection(chatRef, 'messages'), orderBy('timestamp', 'asc'))
      );
    

    可以看到APIhere的正确用法。

    【讨论】:

    • 您是说messagesRes 没问题,但问题出在chatRes 上? @tomerpacific
    • @RonnyFitzgerald - 完全正确。查看我在链接中提到的 API。如果要获取收藏中的所有文档,可以使用:const citiesRef = db.collection('cities'); const snapshot = await citiesRef.get(); snapshot.forEach(doc =&gt; { console.log(doc.id, '=&gt;', doc.data()); });index.js。此示例取自第二个链接。
    【解决方案2】:

    对我不起作用,但我对其进行了一些修改并使用了 firebase 9 集合并在页面加载之前过滤了 context.query.id 对 SSR 有效的集合。

    export async function getServerSideProps(context) {
      const chatID = context.query.id;
      const chatIDRef = collection(db, "chats");
    
      //prep messages
      const messagesRes = await getDocs(chatIDRef, where(chatID, "==", chatID));
      console.log("ctx log: ", messagesRes);
    
      const messages = messagesRes.docs
        .map((doc) => ({
          id: doc.id,
          ...doc.data(),
        }))
        .map((messages) => ({
          ...messages,
          timestamp: messages.timestamp?.toDate().getTime(),
        }));
    
      //prep chat
      const chatIDRef2 = doc(db, "chats", chatID);
    
      const chatRef = await getDoc(chatIDRef2);
    
      const chat = {
        id: chatRef.id,
        ...chatRef.data(),
      };
    
      return {
        props: {
          messages: JSON.stringify(messages),
          chat: chat,
        },
      };
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-12
      • 1970-01-01
      • 2017-01-27
      • 2023-03-30
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多