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

283
Views
How to update a document by ID using Firebase Modular SDK (V9)?

This code creates a new document every time the user logs in but my task is to update existing same user ID document if it exists else create a new one. How can I do that in V9 Firebase?

Current Code

setDoc(
  query(collectionRef),
  //     db.collection('users').doc(user.uid).set(

  {
    email: user.email,
    lastSeen: serverTimestamp(),
    photoURL: user.photoURL
  }, {
    merge: true
  }
);

Old Code that access document UID:

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The first parameter in setDoc() should be DocumentReference:

import { doc, setDoc } from "firebase/firestore"

const docRef = doc(db, "users", user.uid);

setDoc(docRef, {
  email: user.email,
  lastSeen: serverTimestamp(),
  photoURL: user.photoURL
}, {
  merge: true
}).then(() => console.log("Document updated"));
about 4 years ago · Juan Pablo Isaza Report

0

Alternatively, You can also use updateDoc().

import {
  doc,
  updateDoc
} from "firebase/firestore"

update(doc(db, "users", user.uid, {
  email: user.email,
  lastSeen: serverTimestamp(),
  photoURL: user.photoURL
}).then(() => console.log("Document updated"));

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!