I'm trying to do an service function that will check if an specific doc exists or not, I have this in my controller
if (myService.exist()){
// doc Exists
} else {
// doc does NOT Exist
}
myService
has this function:
myService.exist = function(){
myDB.get('config', function(error, response){
if (error){
return false;
} else {
return true;
}
});
};
but I am missing something (probably regarding the PouchDB promise) because I always get undefined in my controller.
I also tried:
myService.exist = function(){
myDB.get('appConfig').then( function() {
return true;
}).catch(function() {
return false
});
};
How can I get this response correctly?
I'm trying to do an service function that will check if an specific doc exists or not, I have this in my controller
if (myService.exist()){
// doc Exists
} else {
// doc does NOT Exist
}
myService
has this function:
myService.exist = function(){
myDB.get('config', function(error, response){
if (error){
return false;
} else {
return true;
}
});
};
but I am missing something (probably regarding the PouchDB promise) because I always get undefined in my controller.
I also tried:
myService.exist = function(){
myDB.get('appConfig').then( function() {
return true;
}).catch(function() {
return false
});
};
How can I get this response correctly?
Share Improve this question asked Oct 5, 2016 at 14:19 distantedistante 7,0256 gold badges61 silver badges103 bronze badges3 Answers
Reset to default 4The issue you are having is due to the fact that pouchDB's get
method is returning a promise, so you cannot have the function return a value from inside of the callback function.
In order to acplish this, you need to have your function return the promise.
myService.exist = function(){
return myDB.get('config');
};
This will ensure that you can call the function and apply the .then
function on the result. However, this means that your function will be resolved or rejected in the same way as the get
function, so you would need to change your calling function to something like:
myService.exist().then(function () {
// doc Exists
}).catch(function () {
// doc does NOT Exist
});
If you want your promise to resolve with a boolean
instead of the document result from pouch, you will need to define a new promise, and return that promise. I usually use a library such as Q to do this, but depending on your support requirements, you could use the Native Javascript library, and it would look something like:
myService.exist = function(){
return myDB.get('config').then(function () {
return Promise.resolve(true);
}).catch(function () {
return Promise.resolve(false);
});
};
And then your calling function would look like this:
myService.exist().then(function (res) {
if (res) {
// doc Exists
} else {
// doc does NOT Exist
}
});
This would ensure that you can have the promise be resolved no matter what since you only care that the document exists, and you don't want to treat a missing document as an error.
You will not be able to have your function return a boolean value if your function relies on a promise, since that promise would need to be resolved to get your answer, and this takes time. When your function is plete (Immediately after calling the get
function, you have not returned anything (since you have your returns in callback functions to be called later. So your function returns undefined.
Promises are pain at the beginning, but are a very helpful tool once you fully understand them.
https://developers.google./web/fundamentals/getting-started/primers/promises
I use this in Vue, you can try the following with async function
myService.exist = async function(){
await myDB.get('config')
.then(function(){ return true;})
.catch(function() { return false; })
};
As per new ES7
standards, your myService.exist can look like this,
myService.exist = async (id) => myDB.get(id);
And while calling this service, use await.
try{
await myService.exist('id');
//reaching here means it exists. write next part of your code here
}catch(err){
//means doc doesn't exist
}
Don't forget to write async
in the function enclosing your controller logic.