It seems like I have done all of the tests for the URL shortener project. I copy and pasted all of my code into repl just to see if it passed all of the test in free code camp and it did. However, when I deploy it on heroku and it fails the third test
" When you visit /api/shorturl/<short_url>
, you will be redirected to the original URL."
The problem is when I delete one character at a time from the url after new url is posted the app crashes and returns the error" Cannot read property ‘original_url’ of undefined"
Here is the code
app.get("/api/shorturl/:shortcut", (req, res) => {
let userGeneratedShortcut = req.params.shortcut;
ShortURL.find({ short_url: userGeneratedShortcut }, (error, url) => {
if (error) return console.log(error);
res.redirect(url[0].original_url);
});
});
The thing I don’t understand is if you select the /new and delete it as a whole and then copy and paste the short_url in that spot then the app successfully takes you to the redirected site. Can someone help make sense of this for me please.
(this same problem happens locally as well so it has to be the code that is the problem and not heroku deployment)
link to project is here: https://repl.it/@Hunterlacefield/boilerplate-project-urlshortener#sample.env