get document by id in firestore code example

Example 1: get one document based on id in firestore

//retrieve one document and save it to userDetails
const [userDetails, setUserDetails] = useState('')
db.collection('users').doc(id).get()
        .then(snapshot => setUserDetails(snapshot.data()))

Example 2: firestore get id of new document

async function addCity(newCity) {
  const { id } = await db.collection("cities").add(newCity)
  console.log("the new city's id:", id)
}

Tags:

Misc Example