Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda