Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda