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 :)