Replit database value equals pending

While trying to create a database for my node.js discord bot, I set the key to the discord users username, and set the value to a inputed value, but when I try to access the the key it logs that the value is Promise { <pending> } I was wondering what may be the reason why this is happening. My code:

if(message.content.startsWith('s!promote') && message.author.username === 'akatech'){
  let user = message.content.split(' ').slice(1).join(' ')
  const Embed = new EmbedBuilder()
      .setColor('#FFD1DC')
            .setTitle('User promotion')
            .setAuthor({ name: `${client.user.username}`, iconURL: "https://cdn.discordapp.com/attachments/1092494309678403646/1131227307244269638/88dc8ffb83f583354a8f0e344d7ce353.webp", url: 'https://starstructbot.repl.co' })
            .setDescription('**' + user + '** has been promoted!!')
            .setTimestamp()
  let msg = await message.channel.send({ embeds: [Embed] })
  msg.react('🥳')
  msg.react('🎉')
  msg.react('🎊')
db.set(`${user}`, "1").then(() => {
message.channel.send(`${db.get(user)}`)
console.log(db.get(`${user}`))
})
}

Thanks in advance!

This is because the replit database is asynchronous, so functions like set and get return a Promise, which represents a “promise” that something is eventually going to finish.

To get the result out of a promise, either call .then with a callback (like you did for db.set) or await the promise, like you did for message.channel.send:

  let msg = await message.channel.send({ embeds: [Embed] })
  msg.react(':partying_face:')
  msg.react(':tada:')
  msg.react(':confetti_ball:')
  await db.set(`${user}`, "1");
  await message.channel.send(`${await db.get(user)}`)
  console.log(await db.get(user))

Also, in the future, please surround your code with triple backticks:

```javascript
code goes here
```
6 Likes

Ohh kk thx! (Also thx for the ``` tip, I didn’t know if that worked here or not :sweat_smile:)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.