URL Shortener Microservice Test Failing but functions properly?

Tell us what’s happening:
Unable to pass 3rd test required redirection to original url with short url. It works on my end if anyone can confirm? Not sure what is going on, have tried the same short urls sent by fcc test suite with success so i am not sure if it is requiring a certain function to pass.

Your project link(s)

solution: https://replit.com/@abelpena/boilerplate-project-urlshortener

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: URL Shortener Microservice

Link to the challenge:

When I log your response from the post route like this:

  await record.save();
  console.log({original_url: req.body.url, short_url:record.short});
  return res.json({original_url: req.body.url, short_url:record.short})

I see

POST /api/shorturl - ::ffff:
  original_url: 'https://boilerplate-project-urlshortener-5.jeremyagray.repl.co/?v=1629507441323',
  short_url: 'd7Pb'
POST /api/shorturl - ::ffff:
  original_url: 'https://boilerplate-project-urlshortener-5.jeremyagray.repl.co/?v=1629507441552',
  short_url: 'd7Pb'

which has repeats of the ID/short_url. Murphy’s law dictates the wrong one will invariably be returned every time unless randomly is harder to debug. The problem is

  short: {type: String, required: true, default: nanoid(4)}

which I assume executes nanoid(4) once, not every time as you want.

This underscores the importance of logging all route inputs and outputs because the problem was found on a GET route but was caused on a POST route. Regrettably, I should be smart enough to follow my own advice, but instead of dutifully logging, I played with the GET route involved with the failing test until I thought “the only thing it can be is duplicate IDs” and then I looked at the DB and saw exactly that. But the logging I used after the fact makes me feel better.

You may have some other issues too. Make sure you return your responses every time (return res.json({...}) for example). I had to modify your CORS setting as well just to get the tests to fail properly.

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