Redirection test fails on url shortener challenge

Tell us what’s happening:
Hello everyone!
I am trying to cope with the url shortener challenge, but I am struggling to pass the redirection test. In my manual tests it seems to redirect to the original url correctly , however the freeCodeCamp tests fail…
On dev tools I can see the redirection code 302 from my server but the test fail somehow…
I have read a lot about people facing difficulties on this but nothing seems to work.
If someone can point out what the problem is I would really appreciate it!

Here is my get method which (makes the redirection but) fails the test :

app.get('/api/shorturl/:id', (req, res) => {

  let original_url;
  let short_url = parseInt(req.params.id);

  // search database for the id entered in the url

  URL.findOne({ "id": short_url }, async (err, data) => {

    (err && console.log('could not reach that id'))

    if (!err && data != null && short_url != undefined) {

      // if such an id was found fetch the original url
      original_url = await data.url;


      // redirect to the url found
      res.redirect(original_url);

    } else {
      res.status(500).json({ error: 'Invalid URL' })
    }
  })

})

Your project link(s)

solution: https://replit.com/@gpprog/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/99.0.4844.51 Safari/537.36

Challenge: URL Shortener Microservice

Link to the challenge:

At this point you need to log your values for short_url, err, and data; you’ll see that your findOne() call is failing. You’ve mixed async and callbacks here so you need to pick one and do that (keep the callback and drop the async or vice-versa). You’ve also implemented your own ID; mongoDB will do that automagically (the _id field, which you already have…).

If you can’t find the error causing the find failure here, check the post route and log your route inputs and outputs to make sure that you are actually saving the URL as you intend.

Thanks for your help! I will keep the callback only as you suggest. However I have no problem adding the correct url to the db when making a post request via the form. In addition, the URL.findOne method, returns the corresponding original_url as expected. So, the redirection works as I can tell, however the 3d test of the challenge fails ! The “id” was added to use an external library ‘mongoose-sequence’ and is working ok for now. ( I suppose I could use the mongo db “_id” variable as you suggest!)

When I forked and ran your code with my DB credentials, it was adding records to the database but logging data at the point mentioned above was logging null. So, the findOne() function was working and there was no error (as such), but data was null and hence, the test fails. Maybe there was a problem with repl.it or mongoDB.com. Retried it today and here’s the output of your logging from the POST route and some extra logging in the GET route.

not on records
https://boilerplate-project-urlshortener-23.jeremyagray.repl.co/?v=1647566211170 was added to database
not on records
https://boilerplate-project-urlshortener-23.jeremyagray.repl.co/?v=1647566211613 was added to database
req.params: {"id":"21"}
url id: https://boilerplate-project-urlshortener-23.jeremyagray.repl.co/?v=1647566211170
record: {
  _id: new ObjectId("6233dd83d6ac453bb7cd0f45"),
  url: 'https://boilerplate-project-urlshortener-23.jeremyagray.repl.co/?v=1647566211170',
  id: 21,
  __v: 0
}

It looks like you are redirected to the URL for the POST test and not the URL for the redirect test. I can’t tell if that’s because you’re using the wrong ID or if the callback/async issue is causing responses to be out of order, but I think it’s the ID since the URL parameter in those URLs is the time in milliseconds and the second one is larger.

I would check for an off-by-one error in the ID or just use the mongoDB native _id.

Thank you so much! I changed the schema to avoid using the custom ‘id’ and it finally passes the tests!