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

540
Views
Cómo verificar si existe un documento de almacenamiento en la nube cuando se usan actualizaciones en tiempo real

Esto funciona:

 db.collection('users').doc('id').get() .then((docSnapshot) => { if (docSnapshot.exists) { db.collection('users').doc('id') .onSnapshot((doc) => { // do stuff with the data }); } });

... pero parece detallado. doc.exists , pero eso no funcionó. Solo quiero verificar si el documento existe, antes de suscribirme a las actualizaciones en tiempo real. Ese get inicial parece una llamada desperdiciada a la base de datos.

¿Hay una mejor manera?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Su enfoque inicial es correcto, pero puede ser menos detallado asignar la referencia del documento a una variable como esta:

 const usersRef = db.collection('users').doc('id') usersRef.get() .then((docSnapshot) => { if (docSnapshot.exists) { usersRef.onSnapshot((doc) => { // do stuff with the data }); } else { usersRef.set({...}) // create the document } });

Referencia: Obtener un documento

over 4 years ago · Santiago Trujillo Report

0

Por favor, compruebe el siguiente código. Puede que te ayude.

 const userDocRef = FirebaseFirestore.instance.collection('collection_name').doc('doc_id'); const doc = await userDocRef.get(); if (!doc.exists) { console.log('No such document exista!'); } else { console.log('Document data:', doc.data()); }
over 4 years ago · Santiago Trujillo Report

0

Sé que es tarde, pero en realidad es snapshot.empty not snapshot.exists . snapshot.empty comprueba si existe un documento y lo devuelve en consecuencia. feliz codificación 😊

over 4 years ago · Santiago Trujillo 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!