How to check the existence of a value in storage?
maryammouse opened this issue · comments
If I simply want to check if an object exists in the store, how do I do that? All I see are get
and create
methods that already factor in the existence of an object.
My specific use case is that if the object doesn't exist, I want to create it with certain values (default values) -- otherwise I want to leave it alone.
I can see this in the example codes:
store.get('key').then((value) => {
if (value !== null) {
// key exists
} else {
// key does not exist
}
});
You can use it to do your thing.
@maryammouse, @aelallam's answer is probably your best bet. There is also a .keys
function you could call to check the existence of a key. You could do something like this:
const keys = await store.keys();
if (keys.indexOf('key') === -1) {
// key does not exist
} else {
// key exists
}
This example assumes you are running this code inside an async
function.