URL shortener crashes if I delete one character from url

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

Try returning the res.json portion when an error is found. It’s possible the code continues to execute and tries to access the invalid index of url. It’s also possible that the DB doesn’t return an error when an invalid shortcut is passed. In this case you will need to check for the presence of url and return an error instead of letting the code redirect.

Ive already tried returning a res.json() instead of console.log(error) and it doesn’t do anything different sadly. If I hit delete and the url ends with “/api/shorturl/ne”
instead of “/api/shorturl/new” the code crashes with the following errors

“node:events:355
throw er; // Unhandled ‘error’ event”
and
“Cannot read property ‘original_url’ of undefined”

I’ve already posted the fallback solution if you read my response entirely.

am I not using callback in the ShortURL.find()? where if there is an error it returns json object?
here is full code if it helps.

app.post("/api/shorturl/new", (req, res) => {
  let clientRequestedURL = req.body.url;
  let shortcut = shortid.generate(); // shortid.generate() is a short non-sequential url-friendly unique id generator

  if (!clientRequestedURL.includes("http")) {
    res.json({ error: "invalid url" });
    return;
  }

  let newURL = new ShortURL({
    original_url: clientRequestedURL,
    short_url: shortcut,
  });

  newURL.save((error, newurl) => {
    if (error) return console.log(error);
    res.json({
      original_url: newurl.original_url,
      short_url: newurl.short_url,
    });
  });
});

app.get("/api/shorturl/:shortcut", (req, res) => {
  let userGeneratedShortcut = req.params.shortcut;
  ShortURL.find({ short_url: userGeneratedShortcut }, (error, url) => {
    if (error) return res.json({ error: "invalid url" });
    res.redirect(url[0].original_url); 
  });
});

So if you look at the error: “Cannot read property ‘original_url’ of undefined”
The runtime is attempting to access a property of an undefined object, which will throw a hard error. Where in the code is the property trying to be accessed? If this object doesn’t have this property, maybe don’t let it access it?

If a user passes an incorrect shortcut value, you don’t expect the server to be able to return a proper URL. Since you’ve tried returning the error which didn’t fix the problem, then mongoose does not return an error and moves on to the next line. With a bad shortcut value, where do you expect the code to redirect the user to?

I implore you to think about this:

1 Like

I see what you’re saying. I fixed the problem. I had to set an asynchronous function and set a variable for find (using await). Then I set an if statement to see if the variable exist to run the code and if not then it would return a json object with error message.

I greatly appreciate your help on this! Also appreciate you not just giving out an answer and making me think about it. You’ve been helpful!

1 Like