Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
What is the version 9 equivalent of this in firebase?

Im trying to convert my version 8 working code to version 9 but I cant figure out why im getting the error: 'FirebaseError: Expected type 'Query', but it was: a custom DocumentReference object'. It seems not like messagesRes but I put in a query and I think im correctly getting the subcollection.

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,
    },
  };
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

According to the API documentation:

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

And in your code you are doing:

const chatRes = await getDocs(chatRef);

That is why you are seeing the error:

'FirebaseError: Expected type 'Query', but it was: a custom DocumentReference object'.

because you are passing in your document reference and not a query.

Like you did here:

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

You can see the correct usage of the API here.

about 4 years ago · Juan Pablo Isaza Relatório

0

Didn't work for me but I modified it a bit and used the firebase 9 collections and filtered the collection where the context.query.id is valid to SSR before the page load.

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,
    },
  };
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda