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

182
Views
Unable to return a default document if not found in PouchDB

I am trying to use PouchDB to store some simple user data and want to create a function that will return a default document if that user doesn't already exist in the database. Thankfully PouchDB show such a function on their online guide, what they show is:

db.get('config').catch(function (err) {
  if (err.name === 'not_found') {
    return {
      _id: 'config',
      background: 'blue',
      foreground: 'white',
      sparkly: 'false'
    };
  } else { // hm, some other error
    throw err;
  }
}).then(function (configDoc) {
  // sweet, here is our configDoc
}).catch(function (err) {
  // handle any errors
});

I then tried to put this into my own function which looked like this:

async function getEntry(user) {
    await db.get(user).catch((err) => {
        if (err.name === 'not_found') {
            return {
                _id: user.id,
                user: user,
                data: {
                counting: {
                    score: 0,
                    fails: 0,
                    highestCounted: 0,
                },
                birthday: "DD/MM/YY"
            }
        } else { throw err }
    }).then((doc) => {
        return doc;
    })
}

However, when calling this function, all I get returned is undefined and don't know what's going wrong;

getEntry({id: "1234"}).then(doc => { console.log(doc); }).catch(err => { console.log(err); } // returns undefined

When I replace the function returns with console.log(), it correctly logs the defaultDoc object so I know that fetching the default doc isn't an issue.

Can someone please explain why this function isn't work and how I can ammend it to get the wanted functionality, many thanks :)

about 4 years ago · Juan Pablo Isaza
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!