Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
¿Cómo configurar el oyente en el estado?

Tengo un oyente de Firestore onSnapshot que escucha los cambios en un documento, pero parece que no puedo entender cómo funciona si lo configuro en el siguiente estado:

 constructor(props) { super(props); this.fetchChatsListener = null; }

y así es como lo configuro:

 this.fetchChatsListener = firestore().collection('chats').doc(documentSnapshot.id).collection('messages').where('created', '>=', currenttime); this.state.sent_message_id != '' ? this.fetchChatsListener.where(firebase.firestore.FieldPath.documentId(), '!=', this.state.sent_message_id) : '' this.fetchChatsListener.orderBy('created', 'desc') .onSnapshot({ error: (e) => console.error(e), next: (querySnapshot_) => { querySnapshot_.docChanges().forEach(change => { this.state.dataSource.push({ ...change.doc.data(), key: change.doc.id, }); }); this.setState({ chatEntered: true, dataSource: this.state.dataSource, refreshingMessages: false, current_chat_id: documentSnapshot.id }); }, });

Llamo a esta función para darme de baja:

 unsubscribe_chats_listener() { this.fetchChatsListener; this.fetchChatsListener = null; }

pero lamentablemente no se da de baja porque sigo recibiendo nuevos cambios.

ACTUALIZAR

cuando trato de darme de baja usando:

 this.fetchChatsListener();

falla con este error:

 TypeError: this.fetchChatsListener is not a function
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El código OP solo menciona la función de cancelación de suscripción, necesita invocarla .

 unsubscribe_chats_listener() { this.fetchChatsListener(); // note the parens, causing invocation this.fetchChatsListener = null; }

EDITAR

this.fetchChatsListener se está inicializando en una consulta. Las consultas no son funciones para darse de baja.

 this.fetchChatsListener = firestore().collection('chats') // a collection ref .doc(documentSnapshot.id) // a doc ref .collection('messages') // a collection ref .where('created', '>=', currenttime); // a query

onSnapshot() devuelve la función de cancelación de suscripción, así que establezca la propiedad allí...

 let query = firestore().collection('chats') .doc(documentSnapshot.id) .collection('messages') .where('created', '>=', currenttime); this.state.sent_message_id != '' ? query.fetchChatsListener.where(firebase.firestore.FieldPath.documentId(), '!=', this.state.sent_message_id) : '' query.orderBy('created', 'desc') this.fetchChatsListener = query.onSnapshot({ // an unsubscribe function error: (e) => console.error(e), next: (querySnapshot_) => { // ...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!