Ask coding questions

← Back to all posts
(Node.JS) Looking to see if a key/value already exists in replit database
h
Itsyourdriver (2)

Alright, I'm working on a discord vanity invite link creator, using the replit db & nodejs. (as a little project)

I just can't figure out how to check if a "slug"/ending has already been made, I know that you can get a key's value just not sure how to make sure it doesn't match a current slug/ending user's input to prevent overwriting. (I'm a beginner to nodejs.)

Current code for creating a link:

// https://replit.com/talk/share/Simple-URL-Shortener-with-Expressjs/39938
// ^ main code is modified off of that
app.post('/result', (req, res) => {
  let listening = db.get(req.body.slug).then(value => {}); //I'm stuck on making sure someone else didn't already make a link with said input slug. (and give them an error if it already exists)
 
  if (req.body && req.body.url.match('https://discord.gg/')) {
   
    let shortenedurl = req.body.slug
    listen(shortenedurl, req.body.url); 
  db.set(shortenedurl, shortenedurl).then(() => {});
    res.send(`Success! Go To: https://discordserver.ga/${shortenedurl}`)
    
  } else if (!req.body || !req.body.url || !req.body.url.match('discord.gg')) {
    
    res.send(`Invalid body or invite. Did you put https://discord.gg/(your invite code) in the input box?`);
    res.end()
  }
});

Thanks!

Answered by Coder100 (18176) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18176)

you could do something like

db.get('<slug>').then(k => { if (k) { /* key exists */ } else { /* key doesn't exist */ } }).catch(() => { /* 
key doesn't exist */ })
Itsyourdriver (2)

@Coder100 Probably a simple syntax fix that I don't really understand but:

 db.get(req.body.slug).then(k => { if (k) { res.send("This ending is already taken, please try another one!")
res.end() } else {  if (req.body && req.body.url.match('https://discord.gg/')) {
    let shortenedurl = req.body.slug
    listen(shortenedurl, req.body.url); 
  db.set(shortenedurl, shortenedurl).then(() => {});
    res.send(`Success! Go To: https://discordserver.ga/${shortenedurl}`)
      }}).catch(() => { if (req.body && req.body.url.match('https://discord.gg/') {
   
    let shortenedurl = req.body.slug
    listen(shortenedurl, req.body.url); 
  db.set(shortenedurl, shortenedurl).then(() => {});
    res.send(`Success! Go To: https://discordserver.ga/${shortenedurl}`) }})
});

I'm getting close but at the same time not really, just having an issue with syntax.

Coder100 (18176)

yes
but sorry for the bad formatting of the code @Itsyourdriver

Coder100 (18176)
app.post('/result', (req, res) => {
  db.get(req.body.slug).then(k => {
    if (k) {
      res.send("This ending is already taken, please try another one!")
      res.end();
    } else {
      if (req.body && req.body.url.match('https://discord.gg/')) {
        let shortenedurl = req.body.slug;
        listen(shortenedurl, req.body.url);
        db.set(shortenedurl, shortenedurl);
        res.send(`Success! Go To: https://discordserver.ga/${shortenedurl}`)
      }
    }).catch(() => {
      if (req.body && req.body.url.match('https://discord.gg/') {

          let shortenedurl = req.body.slug
          listen(shortenedurl, req.body.url);
          db.set(shortenedurl, shortenedurl);
          res.send(`Success! Go To: https://discordserver.ga/${shortenedurl}`)
        }
      })
  });
});

@Itsyourdriver

Itsyourdriver (2)

@Coder100 Sorry for no response, however its still the same error: unexpected token ) at })<-.catch()

Coder100 (18176)

remember: if it is undefined, doing !!value will be false because js.