Skip to content
← Back to Community
Replit database check if key exists
Profile icon

I am trying to do a discord bot, so far so good, but i have only one problem: How can i check if the key i am trying to access exists in the replit database?
if(key exists){
//do sth
somethin like the above.
This is because i need to keep track of user stuff, but i will erase data if i add the set statement everytime the command is ran. This is problematic and bad, so the best solution i have came up with is to check if the database key exists.

Answered by WWEMONSTERMONST [earned 5 cycles]
View Answer
Profile icon

You can use the following code to get all the keys and check if some key exists or not:

// first statement gives the list of keys var keys = db.list().then(keys => { // The function to check if a certain key exists or not and do something. if (keys.includes('key')) { console.log('Key exists'); // do something } else { console.log('Key does not exist'); // do something }; });

Hopefully I was able to help you.