Error in topic 3 of the test URL Shortener Microservice

I have a topic 3 from the URL Shortener Microservice test that I am not able to solve, and it is the only one that presents this error, I checked it in the browser console and it presents the following error:

image

My code:

app.post("/api/shorturl", async function(req, res){
  const url = req.body.url;
  const urlCode = shortid.generate();
  console.log(urlCode)

  if(!validUrl.isWebUri(url)){
    res.json({error: "invalid url"});
  }else{
    try{
      var findOne = await URL.findOne({
        original_url: url
      });
     
      if(findOne){
        res.json({original_url: findOne.original_url,
                 shorturl: findOne.shorturl})
      }else{
        findOne = new URL({
          original_url: url,
          shorturl: urlCode 
        })
        
        await findOne.save();
        
        res.json({
          original_url: findOne.original_url,
          shorturl: findOne.shorturl
        })
      }
    } catch (err){
      console.error(err)
    }
  }
})

app.get("/api/shorturl/:shorturl?", async function(req, res){
try{
      const urlParams = await URL.findOne({
      shorturl: req.params.shorturl
   })
      
  if(urlParams){
    return res.redirect(urlParams.original_url);
  }  
  console.log(req.params.shorturl);
 }catch (err){
   console.log(err)
 }
})

My project: https://boilerplate-project-urlshortener.thijv7.repl.co/

Sorry, I had put this answer in the shorturl to perform a test, I ended up forgetting to remove it, now Iā€™m back to the original function that generates an id for the shorturl key

image

On another note, it should be short_url and not shorturl which you are using as the JSON property name.

1 Like

Thank you very much for the guidance, I managed to solve the challenge!

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