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

418
Views
Cómo actualizar un solo documento de firebase firestore

Después de la autenticación, intento buscar un documento de usuario en /usuarios/, luego me gustaría actualizar el documento con datos del objeto de autenticación, así como algunas propiedades de usuario personalizadas. Pero recibo un error de que el método de actualización no existe. ¿Hay alguna manera de actualizar un solo documento? Todos los ejemplos de documentos de firestore asumen que tiene la identificación del documento real, y no tienen ningún ejemplo de consulta con una cláusula where.

 firebase.firestore().collection("users").where("uid", "==", payload.uid) .get() .then(function(querySnapshot) { querySnapshot.forEach(function(doc) { console.log(doc.id, " => ", doc.data()); doc.update({foo: "bar"}) }); })
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede hacer exactamente lo siguiente ( https://firebase.google.com/docs/reference/js/v8/firebase.firestore.DocumentReference ):

 var db = firebase.firestore(); db.collection("users").doc(doc.id).update({foo: "bar"});
over 4 years ago · Santiago Trujillo Report

0

Compruebe si el usuario ya está allí y luego simplemente .update , o .set si no es así:

 var docRef = firebase.firestore().collection("users").doc(firebase.auth().currentUser.uid); var o = {}; docRef.get().then(function(thisDoc) { if (thisDoc.exists) { //user is already there, write only last login o.lastLoginDate = Date.now(); docRef.update(o); } else { //new user o.displayName = firebase.auth().currentUser.displayName; o.accountCreatedDate = Date.now(); o.lastLoginDate = Date.now(); // Send it docRef.set(o); } toast("Welcome " + firebase.auth().currentUser.displayName); }); }).catch(function(error) { toast(error.message); });
over 4 years ago · Santiago Trujillo Report

0

en su código original cambiando esta línea

 doc.update({foo: "bar"})

a esto

 doc.ref.update({foo: "bar"})

Deberia trabajar

pero una mejor manera es usar la escritura por lotes: https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes

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!