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

174
Views
Check if Firestore document exists and return boolean

I want to make a utility function that returns true or false depending on whether a firestore document in a collection exists.

Below is what I originally wrote, but it returns a promise instead of a boolean.

async function docExists(docName, docId) {
  const docRef = db.collection(docName).doc(docId);

  await docRef.get().then((docSnapshot) => {
    if (docSnapshot.exists) {
      return true
    } else {
      return false
    }
  });
}

Is there a way for it to return a boolean, or is this just the wrong way to approach the problem?

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

0

There's no need to mix the await and .then syntax like you have in your question. This should be sufficient:

async function docExists(docName, docId) {
  const docRef = db.collection(docName).doc(docId);

  let docSnapshot = await docRef.get();
  
  if (docSnapshot.exists) {
    return true;
  } else {
    return false;
  }
}

Alternatively, through the use of promise chaining, you should be able to simply add the return keyword before the original await docRef.get().then(...) that you have in your question.

about 4 years ago · Juan Pablo Isaza Report

0

A simple one liner for that could be:

const docExists = async (docName, docId) => (await db.collection(docName).doc(docId).get()).exists
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!